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)
我的问题是:视图帮助器可以包含多个方法吗?我可以从我的帮助器中调用其他视图帮助器吗?
谢谢
卢卡
发布于 2011-05-13 20:45:39
是的,帮助器可以包含额外的方法。要调用它们,您必须获得帮助器实例。这可以通过在视图中获取助手实例来实现
$genderHelper = $this->getHelper('Gender');
echo $genderHelper->otherMethod();
或者让helper从主helper方法返回自身:
class My_View_Helper_Gender extends Zend_View_Helper_Abstract
{
public function Gender()
{
return $this;
}
// … more code
}
然后调用$this->gender()->otherMethod()
因为视图帮助器包含对视图对象的引用,所以您也可以从视图帮助器中调用任何可用的视图帮助器,例如
public function Gender()
{
echo $this->view->translate('gender');
// … more code
}
发布于 2011-05-13 20:45:48
没有这样的规定,但您可以对其进行自定义。
也许你可以将第一个参数作为函数名传递并调用它。
例如:
$this->公共函数(‘showGender’,$name)
其中showGender将是CommonFunction类中定义的函数,$name将是parametr
发布于 2013-05-21 22:03:56
这是对戈登建议的修改,以便能够使用更多的帮助器实例(每个实例都有自己的属性):
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() {
//...
}
}
你可以这样使用helper:
$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();
编辑:这是一种称为Multiton的设计模式(类似Singleton,但您可以获得更多实例,每个给定的键一个实例)。
https://stackoverflow.com/questions/5991926
复制相似问题