如何减少 Zend 中的类名以从应用程序中的任何位置调用它?
我想遵循 Zend 文件命名约定。
我有 Object.php 文件,其中包含一个类和一个函数。
MyZendProject->application->models->Test->Object.php
class Model_Test_Object {
public function test() {
echo "Test";
}
}
现在我想访问以下文件中的上述测试函数,但没有找到它。
MyZendProject->application->modules->test->controllers->TestController.php
$testModel = new Model_Test_Object();
$testModel->test();
但是当我在 Object.php 中将类命名为 Application_Model_Test_Object
时。 php 并在 TestController 中像这样调用它然后它就可以工作了。
$testModel = new Application_Model_Test_Object();
$testModel->test();
问题是我不想在类名中使用 Application 因为它太长而且难看。有什么方法可以将类命名为 Model_Test_Object 并在应用程序中的任何位置访问它而不包含任何文件。我在 Botstrap.php 的某个地方看到过解决这个问题的方法,但我不记得了,也无法再次找到它。
任何帮助将不胜感激。
谢谢
I want to follow Zend file naming convention.
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();
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();
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.
Any help will be appreciated.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须编写自己的自动加载器
在引导程序中:
这还将为您自己的库注册命名空间 My_
You have to write Your own autoloader
in bootstrap:
This will also register namespace My_ for Your own libraries
这就是它与我一起工作的方式
,然后当你只包含文件时
只需包含“x.php”,它就会工作
that is the way it will work with me
then when u just include the file
just include"x.php" and it will work
尝试将此行添加到您的 configs/application.ini
autoloaderNamespaces[] = "Models"中
Try adding this line into your configs/application.ini
autoloaderNamespaces[] = "Models"
我也不喜欢 ZF 中很长的模型名称,即类 Application_Model_DbTable_SomeTableName
保留 Pear 风格的命名约定和自动发现...这就是我解决它的方法:
1:将以下内容添加到您的 index.php include_path 中:
realpath(APPLICATION_PATH.'../models') //取决于您的index.php文件所在的位置。
2:将此行添加到您的application.ini中
autoloaderNamespaces[] = "Model_"
3: 在模型文件夹中创建一个文件夹,名称为 Model
4: 将模型类命名为 Model_SomeTableName,并将所有模型类保存在新的 models/Model 文件夹中。
现在在你的控制器中你可以简单地调用 $m = new Model_SomeTableName();它会被自动找到并包含在内。
I also don't like the very long model names in ZF i.e. class Application_Model_DbTable_SomeTableName
Retaining Pear-style naming conventions and auto discovery... this is how I solved it:
1: add the following to your index.php include_path:
realpath(APPLICATION_PATH.'../models') //depends on where your index.php file is located.
2: add this line to your application.ini
autoloaderNamespaces[] = "Model_"
3: create a folder inside your models folder and name is Model
4: name your model class like this Model_SomeTableName and save all your model classes in the new models/Model folder.
now in your controller you can simply call $m = new Model_SomeTableName(); and it will be automatically found and included.