Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__autoload disaster - conflicts with Joomla

Tags:

php

autoload

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.

like image 592
jax Avatar asked Jun 14 '10 12:06

jax


1 Answers

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

like image 70
greg0ire Avatar answered Sep 28 '22 09:09

greg0ire