__autoload 灾难 - 与 Joomla 冲突

发布于 2024-09-05 18:18:02 字数 2675 浏览 0 评论 0原文

我刚刚更改了所有代码以使用 __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 />";

我的输出是: 上一篇:我的自动加载: POST: myAutoload:Array

UPDATE 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 正在填充它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝海似她心 2024-09-12 18:18:02

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

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?

慈悲佛祖 2024-09-12 18:18:02

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

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

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

spl_autoload_register('DataMapper::autoload');

I have most simple answer to this question, at least worked for me every time for joomla 1.5.22
::

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

put these two lines before your own autoload register to give joomla preferance in stack

spl_autoload_register('DataMapper::autoload');
嗳卜坏 2024-09-12 18:18:02

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

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

     函数 __autoload {...}
    

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

瞧。完毕。

要阅读其工作原理的清晰简洁的解释,请访问此处:

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

Just for the record, just tried this solution and it works for me in Joomla 1.5:

  1. Go to \libraries\loader.php(160)
    and replace

     function __autoload {...}
    

    with

    spl_autoload_register(array('JLoader', 'load'));
    
  2. Now you've fixed the Joomla side of things. You're actually
    done, there is no step 2. But just for the record, I'd double-check
    that your own libraries also are playing nice and using
    spl_autoload_register. If everyone is playing nice there should be
    no reference to any functions called __autoload anywhere anymore.

Voila. Done.

To read a clear and concise explanation of why this works, go here:

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

淡笑忘祈一世凡恋 2024-09-12 18:18:02

我用其他方式做到了,只需在助手中调用此方法:

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 类都工作正常。

i did it other way, just calling this method in a helper:

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

works all right, doesnt need to change any source of the joomla core, and all zend clases work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文