18
class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
  public function Gender()
  {
    //
  }
}

"The class method (Gender()) must be named identically to the concliding part 
 of your class name(Gender).Likewise,the helper's file name must be named 
 identically to the method,and include the .php extension(Gender.php)"
 (Easyphp websites J.Gilmore)

My question is: Can A view Helper contain more than one method?And can I call other view helpers from within my helper?

thanks

Luca

luca
  • 36,606
  • 27
  • 86
  • 125

3 Answers3

38

Yes, helpers can contain additional methods. To call them, you have to get hold of the helper instance. This can be achieved either by getting a helper instance in the View

$genderHelper = $this->getHelper('Gender');
echo $genderHelper->otherMethod();

or by having the helper return itself from the main helper method:

class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
  public function Gender()
  {
    return $this;
  }
  // … more code
}

and then call $this->gender()->otherMethod()

Because View Helpers contain a reference to the View Object, you can call any available View Helpers from within a View Helper as well, e.g.

 public function Gender()
 {
     echo $this->view->translate('gender');
     // … more code
 }
Gordon
  • 312,688
  • 75
  • 539
  • 559
  • was gonna ask if you could do the second thing, pretty convenient – Ascherer May 15 '11 at 11:13
  • The 'getHelper' approach did not work for me. I had to return the object in the main helper method to allow other helper methods to be called. – webkraller Jun 21 '12 at 16:33
0

This is modification of Gordon's suggestion to be able to use more instances of the helper (each with own properties):

class My_View_Helper_Factory extends Zend_View_Helper_Abstract {
    private static $instances = array();
    private $options;

    public static function factory($id) {
        if (!array_key_exists($id, self::$instances)) {
            self::$instances[$id] = new self();
        }
        return self::$instances[$id];
    }

    public function setOptions($options = array()) {
        $this->options = $options;
        return $this;
    }

    public function open() {
       //...
    }

    public function close() {
       //...
    }
}

You can use helper this way:

$this->factory('instance_1')->setOptions($options[1])->open();
//...
    $this->factory('instance_2')->setOptions($options[2])->open();
    //...
    $this->factory('instance_2')->close();
//...
$this->factory('instance_1')->close();

Edit: This is a design pattern called Multiton (like Singleton, but you can get more instances, one per given key).

Radek Pech
  • 3,032
  • 1
  • 24
  • 29
0

There is no such provision but you can customize it.

May be you can pass the first parameter as function name and call it.

e.g.

$this->CommonFunction('showGender', $name)

here showGender will be function defined in CommonFunction class and $name will be parametr

Jatin Dhoot
  • 4,294
  • 9
  • 39
  • 59