当我不使用 MVC 框架时,如何自动加载 zend 框架类?
最佳答案
Zend 框架的好处是它非常模块化,您可以使用它的任何部分,而无需采用整个框架。
例如,我们可以使用 Zend_Loader_Autoloader
无需使用 Zend_Application
即可设置类自动加载
首先确保 Zend 库在您的包含路径中:
set_include_path('/path/to/zend/' . PATH_SEPARATOR . get_include_path());
然后需要 Autoloader 类:
require_once 'Zend/Loader/Autoloader.php';
然后我们设置自动加载器:
// instantiate the loader
$loader = Zend_Loader_Autoloader::getInstance();
// specify class namespaces you want to be auto-loaded.
// 'Zend_' and 'ZendX_' are included by default
$loader->registerNamespace('My_App_');
// optional argument if you want the auto-loader to load ALL namespaces
$loader->setFallbackAutoloader(true);
一旦设置了自动加载器(最好在 Bootstrap 或其他东西中),您可以调用 Zend 框架类(或您自己的应用程序的类),而无需单独要求它们:
$foo = new Zend_Library_Class();
$bar = new My_App_Class();
在 documentation 中了解更多信息
关于zend-framework - Zend 框架 1.9 : How to use Autoloading without MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1466252/