Who Always Echo "1" For A Global Variable

hi experts,

I want to use global variable to store&read data in Yii.when I access the view.php,the readtimes will be incremented.but it always echo "1" when i access the view.php.

component:

readTimes.php:




  <?php

class ReadTimes extends CApplicationComponent{

	

    private static $readTimes=0;

	

	public static function incReadTimes(){

		self::$readTimes++;

	}

 

	public static function showReadTimes(){

		return self::$readTimes;

	}	

	

}



in main.php:




	'components'=>array(

		'readtime'=>array(

		    'class' => 'ReadTimes',		

		),



in PostController.php:




	public function actionView()

	{

		$post=$this->loadModel();

		$comment=$this->newComment($post);

		Yii::app()->readtime->incReadTimes();

		$this->render('view',array(

			'model'=>$post,

			'comment'=>$comment,

		));

	}



in view.php:




<?php echo Yii::app()->readtime->showReadTimes();?>



Try it as




class ReadTimes extends CApplicationComponent{

        

    private static $readTimes;

        

        public static function incReadTimes(){

            if (self::$readTimes== null){

            self::$readTimes=0;

             }

            self::$readTimes++;

        }

 

        public static function showReadTimes(){

                return self::$readTimes;

        }       

        

}



thanks for your reply.i use your code but it only echo ‘1’ when i refresh the view.php.I want to see where echo 1,2,3,4… when i refresh the view.php.

there is ReadTimes.php:

Each time you refresh the page you’re starting a brand new request and all of the classes are reinitialised. You need to store that value somewhere, probably in the database, if you want it to persist.

i want to store it on RAM. i knew that there is a application scope variable can do this,like asp,java.

it can use Application[‘myval’]=1.but php has no application scope valiable.so i want to know how to do it.can we only use cache to do this?

If you want use this variable only for each user, You can use session variable, in any other case You have to put this variable to db or some file

If you want it in RAM, you should look into using a caching mechanism like APC. This will of course be reset on system reboot or when the cache is purged.

thanks to all,I use CFileCache to resolve this problem,so I can’t setup MemCache.

ReadTimes.php




class ReadTimes{

        

        public static function incReadTimes(){

            $rs=Yii::app()->cache->get("app_readTimes");

            if($rs==null){

            	Yii::app()->cache->set("app_readTimes", "0");

            }else{

            	Yii::app()->cache->set("app_readTimes",$rs+1);

            }

        }

 

        public static function showReadTimes(){

             return Yii::app()->cache->get("app_readTimes");

        }       

        

}



in main.php:




		'cache'=> array(


				'class'=>'system.caching.CFileCache',


		),



in view.php:




<?php echo ReadTimes::showReadTimes();?>



in postController.php




ReadTimes::incReadTimes();