_initX() 函数是否按顺序调用

发布于 2024-10-01 08:21:36 字数 271 浏览 7 评论 0原文

在我的 bootstrap.php 中,我有许多 _initX() 函数,其中一些可能包含依赖于先前 initX 中的代码的代码

protected function _initAutoloading() { }
protected function _initViewInitializer() { }
protected function _initVariables() { }

所以我的问题是,这些 _init 函数是否保证按照它们声明的确切顺序执行?

In my bootstrap.php I have many _initX() functions, and some of them may contain code that depends on code in the previous initX

protected function _initAutoloading() { }
protected function _initViewInitializer() { }
protected function _initVariables() { }

So my question, are these _init functions guaranteed to be executed in the exact order they've been declared?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

坐在坟头思考人生 2024-10-08 08:21:37

阅读手册。有一个称为依赖跟踪的部分:

如果一个资源依赖于另一个资源,它应该在其代码中调用 bootstrap() 以确保该资源已被执行。随后对其的调用将被忽略。

这是示例代码:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        // Ensure the front controller is initialized
        $this->bootstrap('FrontController');

        // Retrieve the front controller from the bootstrap registry
        $front = $this->getResource('FrontController');

        $request = new Zend_Controller_Request_Http();
        $request->setBaseUrl('/foo');
        $front->setRequest($request);

        // Ensure the request is stored in the bootstrap registry
        return $request;
    }
}

您不必依赖订单。

Read the manual. There are a section called Dependency Tracking :

If a resource depends on another resource, it should call bootstrap() within its code to ensure that resource has been executed. Subsequent calls to it will then be ignored.

Here is sample code :

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initRequest()
    {
        // Ensure the front controller is initialized
        $this->bootstrap('FrontController');

        // Retrieve the front controller from the bootstrap registry
        $front = $this->getResource('FrontController');

        $request = new Zend_Controller_Request_Http();
        $request->setBaseUrl('/foo');
        $front->setRequest($request);

        // Ensure the request is stored in the bootstrap registry
        return $request;
    }
}

You don't have to rely on the order.

墨洒年华 2024-10-08 08:21:36

编辑-为了对您的问题提供更直接的答案,我想说它们可能会是因为代码使用 ReflectionObjects::getmethods() 或 get_class_methods 取决于您的 PHP 版本,所以我相信这些会按顺序返回函数,但有PHP 文档或 Zend 文档中没有任何内容可以保证这种情况始终如此,因此我不认为这是受支持的功能。

您可以传递您想要/需要调用的资源函数的名称作为引导程序调用的一部分: $bootstrap->bootstrap(array('foo', 'bar')); 而不是不传递任何内容,让 Zend 应用程序自动调用它们,而您不确定顺序。

但是,如果您的引导资源之间存在依赖关系,我建议您查看资源插件,它允许您将代码分隔在不同的类中,并轻松地从“bar”资源插件代码中调用 $bootstrap('foo') (尽管您也可以使用 _init*() 函数来执行此操作)

资源插件的另一个好处是,如果需要,它们可以与其他引导文件共享,并且它们比 _init*() 函数更容易测试。

确保您阅读了操作理论文档Zend 应用程序 文档

EDIT - To provide a more direct answer to your question, I would say that they probably will be since the code uses ReflectionObjects::getmethods() or get_class_methods depending on your PHP version, so I believe those will return the function in order but there is nothing in the PHP docs or Zend docs that guarantee this will always be the case, so I would not consider this a supported feature.

You can pass the names of the resource functions you want/need to call as part of the bootstrap call: $bootstrap->bootstrap(array('foo', 'bar')); instead of not passing anything and let the Zend Application call them all automatically in which you are not sure of the order.

If you have dependencies in between your bootstrap resources however, I suggest you look at Resource plugins which will allow you to separate your code in different classes and easily call $bootstrap('foo') from within your 'bar' resource plugin code (though you can do so with the _init*() functions as well)

Another benefit of resource plugins is they can be shared with other bootstrap files if you need to and they are easier to test than _init*() functions.

Make sure you read theory of operation document from the Zend Application doc

以可爱出名 2024-10-08 08:21:36

如果您确实需要按特定顺序调用它们,则应该使用辅助列表:

var $init_us = array(
    "_initAutoloading",
    "_initViewInitializer",
    "_initVariables",
);

function __construct() {
    foreach ($this->init_us as $fn) { 
        $this->{$fn}();
    }
}

要在 ZF 中使用该构造,您可以将示例 __construct 重命名为 _initOrderedList 和您的自定义 < code>_initFunctions 到 _myinit... 或其他东西。

If you really need them invoked in a particular order, you should use a helper list:

var $init_us = array(
    "_initAutoloading",
    "_initViewInitializer",
    "_initVariables",
);

function __construct() {
    foreach ($this->init_us as $fn) { 
        $this->{$fn}();
    }
}

To use that construct in ZF you could rename the example __construct into _initOrderedList and your custom _initFunctions into _myinit... or something.

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