<?
class Application{
		
		/**保存共享变量的文件*/
		var $save_file    = 'cache/application';
		/**共享变量的名称*/
		var $application  = null;
		/**序列化之后的数据*/
		var $app_data    = '';
		/**是否已经做过setValue的操作 防止频繁写文件操作*/
		var $__writed    = false;
		
		/**
		* 构造函数
		**/
		function Application(){
				$this->application = array();
		}
		
		/**
		* 设置全局变量
		* @param string $var_name 要加入到全局变量的变量名
		* @param string $var_value 变量的值
		**/
		function set($var_name){
				if (!is_string($var_name) || empty($var_name))
						return false;
				if ($this->__writed){
						$this->application[$var_name] = time();
						return;
				}
				$this->application = $this->get();
				//if (!is_array($this->application))settype($this->application,"array");
				$this->application[$var_name] = time();
				$this->__writed = true;
				$this->app_data = @serialize($this->application);   
				$this->__writeToFile();
		}
		
		/**
		* 删除全局变量
		* @param string $var_name 要删除的全局变量的变量名
		**/
		function Delet($var_name){
				if (!is_string($var_name) || empty($var_name))return false;
				
				$this->application = $this->get();
				
				//if (!is_array($this->application))settype($this->application,"array");
				
				unset($this->application[$var_name]);
				
				$this->__writed = true;
				
				$this->app_data = @serialize($this->application);  
				 
				$this->__writeToFile();
		}
		
		/**
		* 获得在线人数
		* 
		**/
		function Online(){
				$this->application = $this->get();
				foreach($this->application as $key => $val){	
						if(time()-$val > 900){//15分钟内无操作则视为离线
								$this -> Delet($key);
						}
				}
				return count($this->application);
		}
		
		/**
		* 取得保存在全局变量里的值
		* @return array
		**/
		function get(){
		    if (!is_file($this->save_file))
		        $this->__writeToFile();
		    return @unserialize(@file_get_contents($this->save_file));
		}
		
		/**
		 * 写序列化后的数据到文件
		 * @scope private
		 */
		function __writeToFile(){
				$fp = @fopen($this->save_file,"w");
				@fwrite($fp,$this->app_data);
				@fclose($fp);
		}
}
?>