简体   繁体   中英

Zend Framework use mulitple method in a View helper

I was wondering if that was possible to use mulitple function with a View Helper? Let's say I have this view helper :

class Zend_View_Helper_Time extends Zend_View_Helper_Abstract {

    public function time($date) {
        // Some code here
    }
}

In my view, I'm gonna use it like that :

$this->time($some_date);

But what about if I want, in the same helper be able to call another method like :

$this->Time->convertMyDate($some_date);

I tried to do that but unfortunately I've got an error. Are we lock to only use a method name after the class name?

Thanks for your help

I do this, and simply return $this in the constructor:

public function time() {
     return $this;
}

public function convertMyDate($some_date) {
    ...
}

Then:

$this->time()->convertMyDate();

If you want to keep $this->time($some_date) , then you could do as follows (although I think a new method is nicer):

public function time($time = false) {
    if(!$time)
       return $this;
    else {
       ...
    }
}

even if the class worked this call likely wouldn't:

$this->Time->convertMyDate($some_date);

is currently trying to access a public member (Time) of $this:

$this->time()->convertMyDate($some_date);

would now access the convertMyDate() method of helper time() (at least in theory)

It would probably be easiest and more flexible to just build a convertMyDate() helper and use it like:

$this->convertMyDate($this->time($some_date));

It also might be helpful to look at the code for something like the HeadScript() helper as this does the type of thing your looking for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM