用php實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
<?php /** * 功能:實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量 * author: [url]www.itzg.net[/url] * version: 1.0 * 版權(quán):如許轉(zhuǎn)載請(qǐng)保留版權(quán)聲明 */ /*+----------------example---------------------- require_once("Application.php"); $arr = array(0=>"Hi",1=>"Yes"); $a = new Application(); $a->setValue("t1","arui"); $a->setValue("arr",$arr); $u = $a->getValue(); ---------------------------------------------+*/ class Application { /**保存共享變量的文件*/ var $save_file = 'Application/Application'; /**共享變量的名稱*/ var $application = null; /**序列化之后的數(shù)據(jù)*/ var $app_data = ''; /**是否已經(jīng)做過(guò)setValue的操作 防止頻繁寫文件操作*/ var $__writed = false; /** * 構(gòu)造函數(shù) */ function Application() { $this->application = array(); } /** * 設(shè)置全局變量 * @param string $var_name 要加入到全局變量的變量名 * @param string $var_value 變量的值 */ function setValue($var_name,$var_value) { if (!is_string($var_name) || empty($var_name)) return false; if ($this->__writed) { $this->application[$var_name] = $var_value; return; } $this->application = $this->getValue(); if (!is_array($this->application)) settype($this->application,"array"); $this->application[$var_name] = $var_value; $this->__writed = true; $this->app_data = @serialize($this->application); $this->__writeToFile(); } /** * 取得保存在全局變量里的值 * @return array */ function getValue() { if (!is_file($this->save_file)) $this->__writeToFile(); return @unserialize(@file_get_contents($this->save_file)); } /** * 寫序列化后的數(shù)據(jù)到文件 * @scope private */ function __writeToFile() { $fp = @fopen($this->save_file,"w"); @fwrite($fp,$this->app_data); @fclose($fp); } } ?>
該文章在 2012/6/7 2:01:28 編輯過(guò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |