Home  >  Article  >  Backend Development  >  zend怎么自定义像models这样的类自动加载

zend怎么自定义像models这样的类自动加载

WBOY
WBOYOriginal
2016-06-13 13:00:41873browse

zend如何自定义像models这样的类自动加载?
zend的application里有个models的文件夹是存放操作数据库的类,在控制器里直接new一下就可以用了,非常方便,因此本人也新建了一个teaa的文件夹,但不知道为什么,在new的时候老是报找不到类的错误。而且我在bootstrap里已经设置了下面的自动模块加载还是报错找不到类。 
protected function _initAutoloader ()
    {
        $moduleAutoloader = new Zend_Application_Module_Autoloader(array('namespace' => '' , 'basePath' => APPLICATION_PATH));
        return $moduleAutoloader;
    }
最后,我以为是路径没有导入。在引导文件index.php的set_include_path里也加上teaa文件夹的路径,奇怪的是还是报错说找不到类,请高手指点啊。
------解决方案--------------------
默认的命名空间是
Application
Zend

如果想自定义。可以使用application.ini配置文件
例如

<br />
bootstrap.class = "Bootstrap"<br />
appnamespace = "Application"<br />
autoloadernamespaces.test = "Test_"<br />
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"<br />

autoloadernamespaces.test = "Test_"表示自定义的前缀。
例如在项目的/library定义一个TestClass
具体如下:
/library/Test/TestClass.php
文件内容:
<br />
<?php<br />
class Test_TestClass {<br />
	public function show() {<br />
		echo "show";<br />
	}<br />
}<br />

在action中, 就可以使用了
<br />
    public function indexAction()<br />
    {<br />
        $testObj = new Test_TestClass();<br />
        $testObj->show();<br />
        exit;<br />
    }<br />


如果你使用的Module多模块方式,需要使用
Zend_Application_Module_Autoloader以及
Zend_Application_Module_Bootstrap类配合使用。
在_initAutoload可以使用
$this->getApplication ()的
setAutoloaderNamespaces
setIncludePaths

来设置自动加载的命名空间和includepath
可以参考
http://blog.csdn.net/mengxiangbaidu/article/details/7192855
或者官方文档

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn