Zend Framework 1.9:如何在没有 MVC 的情况下使用自动加载 [英] Zend Framework 1.9: How to use Autoloading without MVC

查看:25
本文介绍了Zend Framework 1.9:如何在没有 MVC 的情况下使用自动加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我不使用 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';

然后我们设置自动加载器:

//实例化加载器$loader = Zend_Loader_Autoloader::getInstance();//指定要自动加载的类命名空间.//默认包含 'Zend_' 和 'ZendX_'$loader->registerNamespace('My_App_');//如果您希望自动加载器加载所有命名空间,则为可选参数$loader->setFallbackAutoloader(true);

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

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

文档

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

解决方案

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

这篇关于Zend Framework 1.9:如何在没有 MVC 的情况下使用自动加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆