Skip to content

Quickie Zend Framework Bootstrap Note

Dear Reader,

I’ve been teaching a Zend Framework class this week and my students have been throwing all kinds of questions at me. Most recently, while we were discussing creating a Bootstrap class for an application a question came up about the _init* functions.

The manual states that

$bootstrap->bootstrap();

will fire all of the _init* functions in the bootstrap class. However, the question came up, in what order? Thanks to friends like Rob Allen (author of “Zend Framework in Action“), I was able to give them the answer.

The answer is, usually, in the order of execution, however, these is an exception.

Given this bootstrap class:

< ?php
 
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initDoctype()
        {
            $this->bootstrap('view');
            $view = $this->getResource('view');
            $view->doctype('XHTML1_STRICT');
        }
 
    protected function _initAutoload()
        {
            $autoloader = new Zend_Application_Module_Autoloader(array(
                'namespace' => 'Default_',
                'basePath'  => dirname(__FILE__),
            ));
            return $autoloader;
        }
 
 
    public function _initRoutes()
    {
        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();
        $route = new Zend_Controller_Router_Route(':language/:module/:controller/:action/*',
                                                  array('language'   => 'en',
                                                        'module'     => 'default',
                                                        'controller' => 'index',
                                                        'action'     => 'index'
                                                        )
                                                  );
        $router->addRoute('lang_default', $route);
    } 
 
    Public function _initMyFirstInit()
    {
	/// stuff goes here
    }
 
    Public function _initMySecondInit()
    {
	/// stuff goes here
    }
 
    Public function _initMyThirdInit()
    {
	/// stuff goes here
    }
}

Now, in this example I have 6 _init* functions. When I call

$bootstrap->bootstrap();

in my index.php, it will fire them in the order that they were created.

However, if I have one that needs to be called first, out of order, I can use:

$bootstrap->bootstrap(‘MySecondInit’);

That will fire _initMySecondInit() manually.

Then, when I call

$bootstrap->bootstrap();

It will fire the rest of the methods, in order, skipping _initMySecondInit() because it’s already been called.

That’s an amazing piece of code.

Thanks again to Rob Allen for helping figure this out.

Until Next time
I <3 |<
=C=

3 thoughts on “Quickie Zend Framework Bootstrap Note

  1. It gets even more fun when you throw in the bootstraps for Modules. In general, the main bootstrap fires and then the module bootstraps are called, with the same bypass technique you showed applying. I’m not certain what determines the order of modules, but I suspect it can be manipulated the order they are mentioned in the application config file.

Comments are closed.