PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Helper Initialization

A helper can be initialized in several different ways, based on your needs as well as the functionality of that helper.

The helper broker is stored as the $_helper member of Zend_Controller_Action; use the broker to retrieve or call on helpers. Some methods for doing so include:

  • Explicitly using getHelper(). Simply pass it a name, and a helper object is returned:

    <?php
    $flashMessenger 
    $this->_helper->getHelper('FlashMessenger');
    $flashMessenger->addMessage('We did something in the last request');
  • Use the helper broker's __get() functionality and retrieve the helper as if it were a member property of the broker:

    <?php
    $flashMessenger 
    $this->_helper->FlashMessenger;
    $flashMessenger->addMessage('We did something in the last request');
  • Finally, most action helpers implement the method direct() which will call a specific, default method in the helper. In the example of the FlashMessenger, it calls addMessage():

    <?php
    $this
    ->_helper->FlashMessenger('We did something in the last request');

Note

All of the above examples are functionally equivalent.

You may also instantiate helpers explicitly. You may wish to do this if using the helper outside of an action controller, or if you wish to pass a helper to the helper broker for use by any action. Instantiation is as per any other PHP class.

Zend Framework