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 callsaddMessage():<?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.




