Php 全局/静态函数中的计数器

Php 全局/静态函数中的计数器,php,static,yii,Php,Static,Yii,我希望每次调用actionNewAnswer函数时,计数器都会出现。 但是现在,计数器总是2。怎么了 public function actionNewAnswer(){ static $count = 1; ++$count; $modelAntwoorden = $this->loadModelAntwoorden(); $this->renderPartial('_formAnswer', array( 'model

我希望每次调用actionNewAnswer函数时,计数器都会出现。 但是现在,计数器总是2。怎么了

public function actionNewAnswer(){     
    static $count = 1;
    ++$count;

    $modelAntwoorden = $this->loadModelAntwoorden();

    $this->renderPartial('_formAnswer', array(
        'model' => new VraagAntwoord(), 
        'counter' => $count,
        'modelAntwoorden'=>$modelAntwoorden,
    ),false,true);  
}
actionNewAnswer()
只在一个服务器请求中调用一次,下次是新的

第一,添加到配置中

'cache' => 'CFileCache'
第二,在控制器类中添加常量

const myStaticCount = 'myStaticCount';
第三,添加到actionNewAnswer

$count = 0; // comment for zero +1
if (Yii::app()->cache->offsetExists(self::myStaticCount)) {
    $count = Yii::app()->cache->get(self::myStaticCount);
}
Yii::app()->cache->set(self::myStaticCount, ++$count);

$count
不应该从0开始,以便在第一次调用时转到1吗?@MatteoTassinari是的,它是!但作者有一些奇怪的逻辑,也许这很重要:)谢谢!是,数字为1的计数器由另一种方法生成。