如何减少 Zend 中的类名以从应用程序中的任何位置调用它?

发布于 2024-10-08 16:26:25 字数 1072 浏览 5 评论 0原文

我想遵循 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 技术交流群。

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

发布评论

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

评论(4

晨曦慕雪 2024-10-15 16:26:25

你必须编写自己的自动加载器
在引导程序中:

  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_

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

This will also register namespace My_ for Your own libraries

为人所爱 2024-10-15 16:26:25

这就是它与我一起工作的方式

    set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/models'),
    get_include_path(),
)));

,然后当你只包含文件时

./
/application
 /models
   /x.php
 ...

只需包含“x.php”,它就会工作

that is the way it will work with me

    set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/models'),
    get_include_path(),
)));

then when u just include the file

./
/application
 /models
   /x.php
 ...

just include"x.php" and it will work

十年九夏 2024-10-15 16:26:25

尝试将此行添加到您的 configs/application.ini

autoloaderNamespaces[] = "Models"中

Try adding this line into your configs/application.ini

autoloaderNamespaces[] = "Models"

水晶透心 2024-10-15 16:26:25

我也不喜欢 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.

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