Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • How to Plan a Website
  • Zend Framework
« PHP and Community
Perception and Telecommuting »

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=

Buffer

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

This entry was posted on Thursday, August 13th, 2009 at 9:44 am and is filed under PHP, Programming, zend framework. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Logging In...

Comments are closed.

  • 3 Replies
  • 1 Comment
  • 0 Tweets
  • 0 Facebook
  • 2 Pingbacks
Last reply was August 13, 2009
  1. Cal Evans’ Blog: Quickie Zend Framework Bootstrap Note | Webs Developer
    View August 13, 2009

    [...] Evans has posted a Zend Framework quickie for working with the bootstrap in your application. I’ve been teaching a Zend Framework [...]

  2. MonkeyT
    View August 13, 2009

    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.

  3. Cal Evans’ Blog: Quickie Zend Framework Bootstrap Note | PHP
    View August 13, 2009

    [...] Evans has posted a Zend Framework quickie for working with the bootstrap in your application. I’ve been teaching a Zend Framework [...]

  • Friends of mine

  • My Latest Book


    Avoiding a Goat Rodeo

  • Follow me on twitter!

  • RSS PHP Podcasts

    • Episode 110: Azure hits 1 billion, RubyFlux compiler, Laracasts and more
    • Episode 7: Web Sockets Are Fast
    • Better Documentation for PHP internals – Lately in PHP podcast episode 35
    • Episode 31: Feline Tooth Extraction
    • Episode #2 – Adam Culp
    • Episode 6: PSR-X and the Mexican Standoff
    • Episode 109: Typescript and a bit more…
    • A Better PHP Feature Voting Process – Lately in PHP podcast episode 34
    • Episode 30: It’s Episode 30, you guys
    • PHP Innovation Award Winner of 2012 – Lately in PHP podcast episode 33

  • Me, elsewhere on the Web

    • Best web design company
    • Cal Evans Dot Com
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • Book Review
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me, elsewhere on the Web
    • PHP
    • podcasting
    • Programming
    • SQL
    • Technology
    • Web 2.0
    • wordpress
    • WordPress Plugins
    • writing
    • zend framework


Postcards From My Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).