在我的bootstrap.php中我有很多_initX()函数,其中一些可能包含依赖于前一个initX中的代码的代码
protected function _initAutoloading() { }
protected function _initViewInitializer() { }
protected function _initVariables() { }
所以我的问题是,这些_init函数是否保证按照它们声明的确切顺序执行?
答案 0 :(得分:4)
编辑 - 为了更直接地回答你的问题,我会说他们可能会因为代码使用ReflectionObjects :: getmethods()或get_class_methods而取决于你的PHP版本,所以我相信那些会返回函数订单,但PHP文档或Zend文档中有 nothing ,保证始终如此,所以我不认为这是一个受支持的功能。
您可以将您想要/需要调用的资源函数的名称作为引导调用的一部分传递:$bootstrap->bootstrap(array('foo', 'bar'));
而不是传递任何内容,让Zend应用程序自动调用它们,而您不确定订单。
如果您的引导程序资源之间存在依赖关系,我建议您查看资源插件,它将允许您在不同的类中分隔您的代码,并从“bar”资源插件中轻松调用$ bootstrap('foo')代码(尽管你也可以使用_init *()函数)
资源插件的另一个好处是,如果需要,它们可以与其他引导文件共享,并且它们比_init *()函数更容易测试。
请务必阅读theory of operation doc
中的Zend Application文档答案 1 :(得分:1)
如果您确实需要以特定顺序调用它们,则应使用帮助程序列表:
var $init_us = array(
"_initAutoloading",
"_initViewInitializer",
"_initVariables",
);
function __construct() {
foreach ($this->init_us as $fn) {
$this->{$fn}();
}
}
要在ZF中使用该构造,您可以将示例__construct
重命名为_initOrderedList
,将自定义_initFunctions
重命名为_myinit...
或其他内容。
答案 2 :(得分:1)
阅读manual。有一个名为Dependency Tracking的部分:
如果资源依赖于另一个资源,它应该在其代码中调用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;
}
}
您不必依赖订单。