rwi*_*n04 3 php zend-framework namespaces zend-autoloader
假设我已经在配置文件中为我编写的某些类注册了额外的命名空间"Tracker_"
autoloadernamespaces[]="Tracker_"
Run Code Online (Sandbox Code Playgroud)
具有此命名空间和自动加载器的东西按预期工作,除非我正在测试错误处理.当我测试是否存在不存在的类时,使用
class_exists("Tracker_DoesNotExist");
Run Code Online (Sandbox Code Playgroud)
它引发了一个例外
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
Run Code Online (Sandbox Code Playgroud)
同时,相同的class_exists函数适用于我测试的每个其他情况,即
class_exists("Application_ExistingClass"); //returns true
class_exists("Application_NonExistingClass"); //returns false
class_exists("Tracker_ExistingClass"); //returns true
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?
运行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框架的错误.
希望能为你清理一下.