user505988
user505988

Reputation: 507

Zend View Helper Question

I have finally got a zend view helper working using this in my helper file:

class MY_View_Helper_Table extends Zend_View_Helper_Abstract
{   
    private $table_data="",$table_head="";

    public function Table($data=''){
        return "hello";
    }
}

and this in my view:

print $this->Table();

This just prints out the returned value of the constructor, I think. How do I go about calling other methods of the class? I dont really know how to refer to the instanced object to access its methods.

Upvotes: 1

Views: 413

Answers (1)

prodigitalson
prodigitalson

Reputation: 60413

I have managed to sort of do it using method chaining, in Table I return $this; but there must be a better and normal way of doing it.

Actually no. Thats typically how you do it. Because of how view helpers work, if you need access to other methods on the helper then you either always return $this from your table method or you detect what to invoke by the parameter signature passed to it. For eaxmple:

public function table($options = null)
{
   if(null === $options){
      return $this;
   }

   if(is_array($options)){
     return $this->tableFromArray($options);
   }

   // etc..
}

You can also get the helper instance with $this->getHelper('name') and then chain to the method you want... but IMO thats more confusing than doing parameter detection of just treating the default method as a getter.

Upvotes: 3

Related Questions