如何在Zend中减少我的类名称从应用程序中的任何地方调用它? [英] How can I reduce my Class Name in Zend to call it from any where in application?
问题描述
我想关注 Zend文件命名约定 。
我有 Object.php 文件包含类和函数。
I have Object.php file with a class and a function in it.
MyZendProject-> application-> models-> Test-> Object.php
class Model_Test_Object {
public function test() {
echo "Test";
}
}
现在我想在以下文件中访问上面的测试函数但它没有找到它。
Now I want to access above test function in following file but it is not finding it.
MyZendProject-> application-> modules-> test-> controllers-> TestController.php
$testModel = new Model_Test_Object();
$testModel->test();
但是当我命名类 Application_Model_Test_Object
Object.php ,并像这样在TestController中调用它。
But when I name the class Application_Model_Test_Object
in Object.php and call it in TestController like this then it works.
$testModel = new Application_Model_Test_Object();
$testModel->test();
问题是我不想在类名称中使用应用程序因为它太长和丑陋。是否有任何方式,我命名类 Model_Test_Object
并访问它在应用程序中的任何地方,而不包括任何文件。我已经看到在 Botstrap.php 的某个地方来解决这个问题,但我不记得它,无法再次找到它。
Problem is that I don't want to use Application in class name because it is too long and ugly. Is there any way that I name the class Model_Test_Object
and access it anywhere in application without including any file. I have seen somewhere in Botstrap.php to solve this issue but I did not remember it and unable to find it again.
感谢任何帮助。
感谢
推荐答案
您必须在bootstrap中编写自己的自动加载器
:
You have to write Your own autoloader in bootstrap:
protected function _initAutoload()
{
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('My_');
$resourceLoader = new Zend_Loader_Autoloader_Resource(array(
'basePath' => APPLICATION_PATH,
'namespace' => '',
'resourceTypes' => array(
'form' => array(
'path' => 'forms/',
'namespace' => 'Form_'
),
'model' => array(
'path' => 'models/',
'namespace' => 'Model_'
)
)
));
return $autoLoader;
}
这也会为您自己的库注册命名空间My_
This will also register namespace My_ for Your own libraries
这篇关于如何在Zend中减少我的类名称从应用程序中的任何地方调用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!