planet-ibuildings

Quickie Zend Framework Bootstrap Note

by planet-ibuildings | Comments Off | August 13, 2009

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=

Tags: bootstrap, hints, PHP, Programming, tips, zend framework, zend_application

Related posts

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Most Commented Posts

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.