Zend Framework 1.9:如何在没有 MVC 的情况下使用自动加载

发布于 2024-08-05 11:39:44 字数 37 浏览 4 评论 0原文

当我不使用 MVC 框架时,如何自动加载 zend 框架类?

how do i auto load zend framework classes when i am not using the MVC framework?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2024-08-12 11:39:44

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);

一旦设置了自动加载器(最好是在引导程序或其他东西中),您就可以调用 Zend 框架类(或您自己的应用程序的类),而不必单独要求它们:

$foo = new Zend_Library_Class();
$bar = new My_App_Class();

阅读有关它的更多信息文档

The nice thing about the Zend framework is that it's extremely modular, you can use just about any piece of it you want without adopting the whole thing.

For example, we can use Zend_Loader_Autoloader to set up class auto-loading without having to use Zend_Application

First make sure the Zend library is in your include path:

set_include_path('/path/to/zend/' . PATH_SEPARATOR . get_include_path());

Then require the Autoloader class:

require_once 'Zend/Loader/Autoloader.php';

Then we set up the autoloader:

// 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);

Once the auto-loader is set up (preferably in a bootstrap or something), you can call Zend framework classes (or your own app's classes) without having to require them individually:

$foo = new Zend_Library_Class();
$bar = new My_App_Class();

Read more about it in the documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文