Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • Flex
  • 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=

Related posts

  • Things I learned about Zend Tool (9)
  • php|architect’s Guide to Programming the Zend Framework (8)
  • PHPWomen T-Shirts (1)

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.

3 Responses to “Quickie Zend Framework Bootstrap Note”

  1. Cal Evans’ Blog: Quickie Zend Framework Bootstrap Note | Webs Developer Says:
    August 13th, 2009 at 3:01 pm

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

  2. MonkeyT Says:
    August 13th, 2009 at 5:25 pm

    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 Says:
    August 13th, 2009 at 8:06 pm

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

  • Event Registration Online for Day Camp 4 Developers : Soft Skills

  • Team Based PHP Training

  • Tags

    adobe API article Cal Evans codeworks community conference cw09 Derick Rethans developers devzone elizabeth naramore Exim flex fun IBuildings Kathy Evans linkedin Management Marketing microsoft MySQL Nashville open source phar PHP phparchitect php developers podcampnashville podcast Programming Quickies respect Sebastian Bergmann Silly-Con Valley sixty second tech software development terry chay twitter upgrade video windows wordpress zend zend framework

  • RSS PHP Podcasts

    • JSClasses, JSMag, PHP Alpha 1, PHP strict typing, IndieConf – Lately in PHP podcast episode 4
    • SitePoint Podcast #76: Wicked WordPress Themes with Allan Cole and Jeffrey Way
    • DPCRadio: Designing for Reusability
    • SitePoint Podcast #75: Awesome Overkill
    • DPCRadio: Technical Debt
    • SitePoint Podcast #74: WordPress Themes with Nathan Rice and Cory Miller
    • SitePoint Podcast #73: Cease and Desoup
    • DPCRadio: APC & Memcache the High Performance Duo
    • APC & Memcache the High Performance Duo
    • SitePoint Podcast #72: Web Video and Social Media with Gregory Ng and Wayne Sutton

  • Me

    • Best web design company
    • Cal Evans Dot Com
    • Cheap Wine Diary
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts
    • Sixty Second Tech

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me
    • 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).