简体   繁体   中英

Can not call many function : Zend_View_Helper

Can not call many function: Zend_View_Helper

helpers : MainHelpers.php

Class Zend_View_Helper_MainHelpers {
  public function mainHelpers(){
    $output="ok 1";
    return $output;
}

  public function mainHelpers2(){
    $output="ok 2";
    return $output;
  } 
}

view : detail.phtml

 <?php echo $this->mainHelpers(); ?>  // ok call function
 <?php echo $this->mainHelpers2(); ?> // not ok call function

I want to call many function in zend_view_helper .

If you want your view helper to contain additional methods besides its constructor, make sure you return the object instance and do something like this:

Class Zend_View_Helper_MainHelpers {
  public function mainhelpers() {
      return $this;
  }
  public function foo(){
    $output="ok 1";
    return $output;
  }

  public function bar(){
   $output="ok 2";
   return $output;
  } 
}

Now call your helper methods like this:

$this->mainhelpers()->foo()
$this->mainhelpers()->bar()

Judging from your code example, it seems you are trying to encapsulate more than one view helper inside one class. You are probably better of by creating more view helpers:

class Zend_View_Helper_Foo()
{
     public function foo() 
     {
         // do stuff
     }
}

class Zend_View_Helper_Bar()
{
     public function bar() 
     {
         // do more stuff
     }
}

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