Php 如何将zend helper转换为singleton?
我需要使用一个视图帮助器在一堆不同的部分中进行计数。 在部分中,我不能访问视图变量,但我可以访问帮助程序,所以我创建了这个简单的类Php 如何将zend helper转换为singleton?,php,zend-framework,singleton,helper,zend-view,Php,Zend Framework,Singleton,Helper,Zend View,我需要使用一个视图帮助器在一堆不同的部分中进行计数。 在部分中,我不能访问视图变量,但我可以访问帮助程序,所以我创建了这个简单的类 class Zend_View_Helper_Counter extends Zend_View_Helper_Abstract{ protected $count = 0; public function counter(){ return $this; } public function add($i = 1){
class Zend_View_Helper_Counter extends Zend_View_Helper_Abstract{
protected $count = 0;
public function counter(){
return $this;
}
public function add($i = 1){
$this->count = $this->count + (int) $i;
return $this;
}
public function get(){
return $this->count;
}
public function set($count){
$this->count = (int) $count;
return $this;
}
}
但是,此
始终返回1。我猜这是因为它总是类的不同实例。我需要如何更改counter()
函数,以便它可以对所有视图和部分进行计数
static protected $count = 0;
public function add($i = 1){
self::$count = self::$count + (int) $i;
return $this;
}
public function get(){
return Counter::getInstance();
}
public function add($i = 1){
Counter::getInstance()->add($i);
return $this;
}
static protected $count = 0;
public function add($i = 1){
self::$count = self::$count + (int) $i;
return $this;
}
public function get(){
return Counter::getInstance();
}
public function add($i = 1){
Counter::getInstance()->add($i);
return $this;
}
如果需要,还可以通过使用命名计数器对其进行扩展,然后$count将成为一个数组。2通过使用单独的单例类实现与1相同的操作-请参见此处:2通过使用单独的单例类实现与1相同的操作-请参见此处:不要为此使用其他单例。您可以使用引导参数或Zend注册表对象来存储计数器。不要为此使用其他单例。您可以使用引导参数或Zend注册表对象来存储计数器。