__autoload 灾难 - 与 Joomla 冲突
我刚刚更改了所有代码以使用 __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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该决定哪个自动加载函数应优先于另一个函数,并相应地使用 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?我对这个问题有最简单的答案,至少每次 joomla 1.5.22 都对我有用
::
将这两行放在您自己的自动加载寄存器之前,以在堆栈中给予 joomla 优先权
I have most simple answer to this question, at least worked for me every time for joomla 1.5.22
::
put these two lines before your own autoload register to give joomla preferance in stack
只是为了记录,刚刚尝试了这个解决方案,它在 Joomla 1.5 中对我有用:
转到 \libraries\loader.php(160)
并替换
与
完成,没有第 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:
Go to \libraries\loader.php(160)
and replace
with
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/
我用其他方式做到了,只需在助手中调用此方法:
工作正常,不需要更改 joomla 核心的任何源,并且所有 zend 类都工作正常。
i did it other way, just calling this method in a helper:
works all right, doesnt need to change any source of the joomla core, and all zend clases work fine.