如果存在不存在的类,为什么 zend autoloader 测试会产生错误?
假设我已经在配置文件中为我编写的一些类注册了额外的命名空间“Tracker_”,使用
autoloadernamespaces[]="Tracker_"
具有此命名空间的事物和自动加载器按预期工作,除非我正在测试错误处理。当我测试不存在的类是否存在时,使用
class_exists("Tracker_DoesNotExist");
它会引发异常
include_once(Tracker/DoesNotExist.php): failed to open stream: No such file or directory
/path/Zend/Loader.php:146
/path/Zend/Loader.php:146
/path/Zend/Loader.php:94
/path/Zend/Loader/Autoloader.php:479
/path/Zend/Loader/Autoloader.php:124
/other/path/TrackablesMapper.php:40 //line referenced above
同时,相同的 class_exists 函数适用于我测试过的所有其他情况,即
class_exists("Application_ExistingClass"); //returns true
class_exists("Application_NonExistingClass"); //returns false
class_exists("Tracker_ExistingClass"); //returns true
我做错了什么吗?
Say I've registered the extra namespace "Tracker_" in the config file for some classes I've written, using
autoloadernamespaces[]="Tracker_"
Things with this namespace and autoloader work as expected except when I am testing for error handling. When I test whether a non-existing class exists, using
class_exists("Tracker_DoesNotExist");
It throws an exception
include_once(Tracker/DoesNotExist.php): failed to open stream: No such file or directory
/path/Zend/Loader.php:146
/path/Zend/Loader.php:146
/path/Zend/Loader.php:94
/path/Zend/Loader/Autoloader.php:479
/path/Zend/Loader/Autoloader.php:124
/other/path/TrackablesMapper.php:40 //line referenced above
Meanwhile, the same class_exists function works for every other case I've tested, i.e.
class_exists("Application_ExistingClass"); //returns true
class_exists("Application_NonExistingClass"); //returns false
class_exists("Tracker_ExistingClass"); //returns true
Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行 Zend Framework 应用程序时,它使用
spl_autoload_register
注册其自动加载器 (http://php.net/ spl_autoload_register)。现在对 class_exists 的任何调用都将使用 Zend 的自动加载器(默认情况下class_exists
尝试加载该类)。当使用 class_exists 与
Tracker_
而不是Application_
时出现错误的原因是应用程序命名空间的自动加载器由Zend_Application_Module_Autoloader
(Zend_Loader_Autoloader_Resource) 处理,与Zend_Loader
自动加载器的行为略有不同。Zend_Loader
执行一些基本的安全检查,然后尝试包含有问题的文件。资源自动加载器实际上使用一种方法,首先检查要自动加载的文件是否可读,如果不可读,则不会尝试包含它。因此,您收到
Tracker_
错误的原因是因为尝试自动加载时没有执行错误检查,而Application_
确实进行了错误检查。您还可以通过调用 Zend_Loader_Autoloader::getInstance()->suppressNotFoundWarnings(true); 来抑制此功能,但通常您不想打开此功能,因为它会在以后造成更多混乱。
类存在会调用自动加载器,因为如果包含该类的文件尚未被包含,则该类不存在,因此需要先尝试尝试加载它,如果自动加载失败,则得到包含来自 zend 框架的错误。
希望这对你来说有点清楚了。
When running a Zend Framework application, it registers its autoloader using
spl_autoload_register
(http://php.net/spl_autoload_register). Now any calls to class_exists will use Zend's autoloader (by defaultclass_exists
tries to load the class).The reason you are getting the error when using class_exists with
Tracker_
and notApplication_
is because the Application namespace's autoloader is handled byZend_Application_Module_Autoloader
(Zend_Loader_Autoloader_Resource) which acts slightly different than theZend_Loader
autoloader.Zend_Loader
performs some basic security checks and then simply tries to include the file in question. The resource autoloader actually uses a method that first checks to see if the file to be autoloaded is readable and if it is not, then it does not try to include it.So the reason you are getting the error with
Tracker_
is because no error checking is performed when trying to autoload, andApplication_
does have error checking.You can also suppress this by calling
Zend_Loader_Autoloader::getInstance()->suppressNotFoundWarnings(true);
Usually you don't want to turn this on though as it can create more confusion later.Class exists will call the autoloader because if the file containing the class has not yet been included, then the class does not exist, so it needs to attempt to try to load it first, if it fails to autoload it, then you get the include error from zend framework.
Hope that cleared it up a bit for you.
您已告诉 Zend 自动加载器需要该名称空间内文件中的任何类。
class_exists()
文档 触发自动加载器。如果您想防止这种情况发生,请添加另一个参数:如果您不想从
Tracker_
命名空间(类前缀)自动加载类,请不要将其注册到自动加载器。You have told the Zend autoloader to require any class from a file within that namespace.
class_exists()
Docs triggers the autoloader. If you would like to prevent that, add another parameter:If you don't want to autoload classes from the
Tracker_
namespace (class prefix), don't register it with the autoloader.