[英]PHP5 AutoLoader SPL_AutoLoad Fails
I have the code below as an autoload class, however it appears that the clean
method simply isn't working and it always falls back on the dirty
method. 我将下面的代码作为自动加载类,但是似乎clean
方法根本无法正常工作,并且总是落在dirty
方法上。
Am I using spl_autoload incorrectly? 我使用spl_autoload不正确吗? If so what is the correct (better) way? 如果是这样,正确(更好)的方法是什么? Is this inefficient, how could it be improved? 这种效率低下,如何加以改进?
I always get output such as the bottom when using this method though, and in some cases it just doesn't find the class but doesn't throw any error I have display errors set to 1 and have checked the error log but just completely missing. 使用此方法时,我总是会得到诸如底部的输出,在某些情况下,它只是找不到类但没有引发任何错误,我将显示错误设置为1,并检查了错误日志,但完全丢失了。
The code gets initialised as 该代码被初始化为
require "vendor/AutoLoader.class.php";
self::setGlobal("autoloader", AutoLoader::init());
And the class is as follows: 并且该类如下:
public static $instance;
private $_src=array('vendor/', 'lib/', '');
private $_sub=array('base/', '');
private $_ext=array('.php', 'class.php', 'lib.php');
/* initialize the autoloader class */
public static function init(){
if(self::$instance==NULL){
self::$instance=new self();
}
return self::$instance;
}
/* put the custom functions in the autoload register when the class is initialized */
private function __construct(){
spl_autoload_register(array($this, 'clean'));
spl_autoload_register(array($this, 'dirty'));
}
/* the clean method to autoload the class without any includes, works in most cases */
private function clean($class){
$class=str_replace('_', '/', $class);
spl_autoload_extensions(implode(',', $this->_ext));
foreach($this->_src as $resource){
foreach($this->_sub as $sub){
echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
set_include_path(pegFramework::getGlobal("baseDir") . $resource.$sub);
spl_autoload($class);
if(class_exists($class)) {
echo 'Found and clean included '.$class.' in '.$resource.$sub."<br />";
break 2;
}
}
}
}
/* the dirty method to autoload the class after including the php file containing the class */
private function dirty($class){
global $docroot;
$class=str_replace('_', '/', $class);
foreach($this->_src as $resource){
foreach($this->_ext as $ext){
foreach($this->_sub as $sub){
echo 'Trying to load ', $class, ' via ', __METHOD__, "()<br />";
if(@include(pegFramework::getGlobal("baseDir") . $resource . $sub. $class . $ext)) {
echo 'Found and dirty included '.$class.' as '.$resource . $sub. $class . $ext."<br />";
break 3;
}
}
}
}
spl_autoload($class);
}
Trying to load pegDatabase via pegAutoloader::clean()
...snip...
Trying to load pegDatabase via pegAutoloader::clean()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load pegDatabase via pegAutoloader::dirty()
Trying to load basepegDatabase via pegAutoloader::clean()
...snip...
Trying to load basepegDatabase via pegAutoloader::clean()
Trying to load basepegDatabase via pegAutoloader::dirty()
Found and dirty included basepegDatabase as vendor/base/basepegDatabase.php
Found and dirty included pegDatabase as vendor/pegDatabase.php
Trying to load pegRequest via pegAutoloader::clean()
...snip...
Trying to load pegRequest via pegAutoloader::clean()
Trying to load pegRequest via pegAutoloader::dirty()
Trying to load pegRequest via pegAutoloader::dirty()
Found and dirty included pegRequest as vendor/pegRequest.php
Trying to load pegFacebook via pegAutoloader::clean()
...snip...
Trying to load pegFacebook via pegAutoloader::clean()
Trying to load pegFacebook via pegAutoloader::dirty()
...snip...
Trying to load pegFacebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::clean()
Trying to load Facebook via pegAutoloader::clean()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
Trying to load Facebook via pegAutoloader::dirty()
...snip...
Trying to load Facebook via pegAutoloader::dirty()
I think it's the call to spl_autoload
that's tripping you up. 我认为这是对spl_autoload
的调用使您spl_autoload
。 In the comments of the php documentation , I found this : 在php文档的注释中,我发现了这一点 :
Note this function will LOWERCASE the class names its looking for, dont be confused when it cant find Foo_Bar.php 请注意,此函数将小写其查找的类名,当找不到Foo_Bar.php时不要混淆
So, calls to spl_autoload
with pegFacebook
or Facebook
are just going to trigger searches for files named pegfacebook.php
or facebook.class.php
. 因此,使用pegFacebook
或Facebook
调用spl_autoload
pegFacebook
触发搜索名为pegfacebook.php
或facebook.class.php
文件。
If I were you, though, I would look at Symfony's UniversalClassLoader for examples of much more simplified handling of class loading. 但是,如果我是您,我将看一下Symfony的UniversalClassLoader,以获取更为简化的类加载处理示例。 You could probably tweak it to use specific extensions, but I would at least think about finding a new way to handle this problem. 您可能会对其进行调整以使用特定的扩展名,但我至少会考虑寻找一种新的方式来处理此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.