5

我刚刚更改了所有代码以使用 __autoload 发现它与 joomla 自动加载器冲突。在某些情况下,我将我的应用程序与 joomla 集成以注册用户等。

我发现 spl_autoload_register() 显然允许许多自动加载器。

我应该怎么办?

更新:这就是 joomla 所做的

从 /library/loader.php 加载这个文件

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

更新 2:

好的,在我加载我调用的 Joomla 库之后

    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');

这是我的自动加载的样子:

<?php

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

?>

spl_autoload_register('myAutoload',false,true);

我的首先被调用,然后 joomla 被调用,但是,该应用程序仍然找不到 Joomla 类。

更新 3:

运行后:

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

    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 />";

我的输出是:PRE:myAutoload:POST:myAutoload:Array

更新 4:

我不得不将 Joomla 导入更改为:

    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' );

这是输出

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:

在我包含这些 Joomla 文件后,我已经解决了

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

spl_autoload_functions() 什么都不返回,所以 joomla 不知何故把它填满了。

4

4 回答 4

3

您应该决定哪个自动加载功能应该优先于另一个,并spl_autoload_register()相应地使用(查看第三个参数)。Joomla 自动加载功能是如何注册的?

于 2010-06-14T13:07:21.963 回答
2

只是为了记录,刚刚尝试了这个解决方案,它在 Joomla 1.5 中对我有用:

  1. 转到 \libraries\loader.php(160) 并替换

     function __autoload {...}
    

    spl_autoload_register(array('JLoader', 'load'));
    
  2. 现在您已经修复了 Joomla 方面的问题。您实际上已经完成,没有第 2 步。但为了记录,我会仔细检查您自己的库是否也运行良好并使用 spl_autoload_register。如果每个人都玩得很好,那么任何地方都不应再引用任何名为 __autoload 的函数。

瞧。完毕。

要阅读有关其工作原理的清晰简洁的解释,请转到此处:

http://www.nicollet.net/2010/06/autoloading-be-friendly-to-intruders/

于 2011-07-26T09:30:07.137 回答
2

我对这个问题有最简单的答案,至少每次都为我工作 joomla 1.5.22 ::

require_once ( JPATH_SITE .DS.'libraries'.DS.'loader.php' );
spl_autoload_register('__autoload');

将这两行放在您自己的自动加载寄存器之前,以在堆栈中给予 joomla 优先权

spl_autoload_register('DataMapper::autoload');
于 2011-03-12T13:44:54.463 回答
0

我以其他方式做到了,只是在帮助程序中调用此方法:

public static function configureZend(){
    $params = self::getParameters();
    $zf_path = $params->get('zend_location');//this should be the zend framework directory location
    define( 'ZFLIBPATH', $zf_path );
    set_include_path( ZFLIBPATH );
    require_once 'Zend/Loader/Autoloader.php';
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->pushAutoloader(array('JLoader','load'),'');
}

可以正常工作,不需要更改 joomla 核心的任何来源,并且所有 zend 类都可以正常工作。

于 2011-02-05T20:00:20.123 回答