简体   繁体   English

Zend_Loader包含不会引发异常

[英]Zend_Loader include doesn't throw exception

I am finding that when Zend tries to auto load a file which doesn't exist, it throws an error which I cannot catch in a try/catch block. 我发现,当Zend尝试自动加载不存在的文件时,它会引发错误,我无法在try / catch块中捕获该错误。 This happens when I use class_exists too. 当我也使用class_exists时,会发生这种情况。 I have fixed the problem by hacking zend: 我通过黑客入侵解决了这个问题:

if ($once) {
    if (!@include_once ($filename)) {
        throw new Exception("Failed to include $filename");
    }
    // include_once $filename;
}
else {
    if (!@include ($filename)) {
        throw new Exception("Failed to include $filename");
    }
    // include $filename;
}

The commented out lines are zend's originals. 注释掉的线条是zend的原始作品。 Now I can catch the exception thrown when a file cannot be included. 现在,我可以捕获无法包含文件时引发的异常。 Can anybody suggest a cleaner way to do this which doesn't involve hacking zend? 有人可以建议一种更清洁的方式来进行黑客入侵吗?

I am on Zend version 1.11.10, and the code in question is Zend_Loader line 146. 我使用的是Zend版本1.11.10,所讨论的代码是Zend_Loader第146行。

Thanks. 谢谢。

instead of using include or include_once try using Zend_Loader::loadClass() 而不是使用include或include_once尝试使用Zend_Loader::loadClass()

Here is the API: Zend_Loader::loadClass($class, $dirs) 以下是API: Zend_Loader::loadClass($class, $dirs)

An example: 一个例子:

Zend_Loader::loadClass('Container_Tree',
    array(
        '/home/production/mylib',
        '/home/production/myapp'
    )
);

Now the blurb on how it works: 现在介绍一下它是如何工作的:

The string specifying the class is converted to a relative path by substituting underscores with directory separators for your OS, and appending '.php' . 通过使用操作系统的目录分隔符将下划线替换为下划线,并附加“ .php” ,可将指定类的字符串转换为相对路径。 In the example above, *'Container_Tree'* becomes 'Container\\Tree.php' on Windows. 在上面的示例中,*'Container_Tree'*在Windows上变为'Container \\ Tree.php'

If $dirs is a string or an array, *Zend_Loader::loadClass()* searches the directories in the order supplied. 如果$ dirs是字符串或数组,则* Zend_Loader :: loadClass()*将按提供的顺序搜索目录。 The first matching file is loaded. 加载第一个匹配文件。 If the file does not exist in the specified $dirs , then the include_path for the PHP environment is searched. 如果指定的$ dirs中不存在该文件,则搜索PHP环境的include_path。

If the file is not found or the class does not exist after the load, *Zend_Loader::loadClass()* throws a Zend_Exception . 如果在加载后未找到文件或该类不存在,则* Zend_Loader :: loadClass()*会引发Zend_Exception

This should allow you to use a try/catch block for any non-existent classes. 这应该允许您将try / catch块用于任何不存在的类。 Zend_Loader::loadFile() also has similar functionality. Zend_Loader :: loadFile()也具有类似的功能。

Don't try and autoload classes that don't exist. 不要尝试自动加载不存在的类。 If for some reason the class you're trying to autoload may or may not be there, wrap that part of code with a class_exists() call. 如果由于某种原因您试图自动加载的类可能存在或可能不存在,请使用class_exists()调用包装该部分代码。

I can't think of any reason why you would want class_exists() to throw an exception on failure since it's sole purpose is to allow you to check for the existence of classes. 我想不出为什么要class_exists()在失败时引发异常的任何原因,因为它的唯一目的是允许您检查类的存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM