tags:

views:

145

answers:

1

I have just changed all my code to use __autoload to find that it conflicts with the joomla autoloader. I integrate my app with joomla in some cases to register users etc.

I found spl_autoload_register() with aparently allows many autoloaders.

What should I do?

update: this is what joomla does

Loads this file from /library/loader.php

function __autoload($class)
{
    if(JLoader::load($class)) {
        return true;
    }
    return false;
}

Update 2:

OK, right after I load the Joomla library I call

    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

    //autoloader so that it does not interfere with mine
    spl_autoload_register('__autoload');

This is what my autoload looks like:

<?php

//IMPORT
function myAutoload($class_name)
{
    $path = str_replace('_', '/', $class_name);
    include $path . '.php';
}

?>

spl_autoload_register('myAutoload',false,true);

Mine gets called first and the joomla one second, however, the the app still can't find the Joomla classes.

Update 3:

After running:

echo "PRE: myAutoload:" . spl_autoload_functions() . "<br />";
spl_autoload_register('myAutoload',false,true);
echo "POST: myAutoload:" . spl_autoload_functions() . "<br />";

and

    echo "PRE: JoomlaAutoLoad:" . spl_autoload_functions() . "<br />";
    //autoloader so that it does not interfere with mine
    spl_autoload_register('__autoload');
    echo "POST: JoomlaAutoLoad:" . spl_autoload_functions() . "<br />";

My output was: PRE: myAutoload: POST: myAutoload:Array

UPDATE 4:

I had to change the Joomla imports to this:

    require_once ( JPATH_BASE .DS.'libraries'.DS.'loader.php' );
    echo "PRE: JoomlaAutoLoad:" . var_dump(spl_autoload_functions()) . "<br />";
    //autoloader so that it does not interfere with mine
    spl_autoload_register('__autoload');

            echo "POST: JoomlaAutoLoad:" . var_dump(spl_autoload_functions()) . "<br />";

    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

Here is the output

PRE: myAutoload:
array
  0 => string 'myAutoload' (length=10)
POST: myAutoload:
array
  0 => string 'myAutoload' (length=10)
PRE: JoomlaAutoLoad:
array
  0 => string 'myAutoload' (length=10)
  1 => string '__autoload' (length=10)
POST: JoomlaAutoLoad:

I have worked out that after I include these Joomla files

    require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
    require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

The spl_autoload_functions() returns nothing, so somehow joomla is stuffing it up.

+1  A: 

Hello, you should decide which autoload function should have priority over the other one, and user spl_autoload_register() accordingly (look at the third argument). How is the Joomla autoload function registered?

greg0ire
I don't know how joomla is registered. I know the file the autoloader comes from, but that is about all. I will have a look now.
jax
Ok, spl_autoload_register() is not used here, the conventional __autoload naming makes the registration automatic I suppose... let's hope this is compatible with spl_autoload_register(). Look at the documentation for spl_autoload_functions(), it says something about an autoload stack, which must be turned on. I think you should see if you get something else than false when you call this function after Joomla is loaded.
greg0ire