_initX() 函数是否按顺序调用
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
阅读手册。有一个称为依赖跟踪的部分:
这是示例代码:
您不必依赖订单。
Read the manual. There are a section called Dependency Tracking :
Here is sample code :
You don't have to rely on the order.
编辑-为了对您的问题提供更直接的答案,我想说它们可能会是因为代码使用 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
如果您确实需要按特定顺序调用它们,则应该使用辅助列表:
要在 ZF 中使用该构造,您可以将示例
__construct
重命名为_initOrderedList
和您的自定义 < code>_initFunctions 到_myinit...
或其他东西。If you really need them invoked in a particular order, you should use a helper list:
To use that construct in ZF you could rename the example
__construct
into_initOrderedList
and your custom_initFunctions
into_myinit...
or something.