initialize() callback for components

Published on and tagged with cakephp  component

In CakePHP 1.2 a new callback function for components has been introduced: initialize(). I am not sure what its purpose is, as there is already a startup() callback function (which is called after the initialize() function). Anyway, here is how you can define the callback function:

function initialize(&$controller) {
    // do some init stuff
}

Thanks to Dieter@be for mentioning this callback in a comment.

3 comments baked

  • Dieter@be

    well it could always disappear again off course, maybe this is just temporary or something. I found this accidentally when researching cake’s new auth component

    bake on !

  • nate

    Nope, this callback was a planned addition for a while, it’s not going anywhere. In fact, there are a couple more component callbacks planned for 1.2, as well as some new ones for helpers.

    As far as the reason why it is there, it was added, as the name implies, so components could do basic initialization. It should not be used for taking any automatic actions (like startup()), but simply so the component can make itself available for use, specifically in beforeFilter().

    This way, if a component needs to initialize itself with data from the controller before being used in beforeFilter, it can do this automatically, rather than forcing users to add redundant code to their beforeFilter() methods.

  • cakebaker

    @Dieter, nate: Thanks for your comments and your additional information.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License