用php實(shí)現(xiàn)像JSP,ASP里Application那樣的全局變量
更新時(shí)間:2007年01月12日 00:00:00 作者:
復(fù)制代碼 代碼如下:
<?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)做過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);
}
}
?>
相關(guān)文章
php讀取csv數(shù)據(jù)保存到數(shù)組的方法
這篇文章主要介紹了php讀取csv數(shù)據(jù)保存到數(shù)組的方法,通過封裝的類文件實(shí)現(xiàn)這一功能,是對(duì)csv文件操作的實(shí)用技巧,需要的朋友可以參考下2015-01-01PHP 循環(huán)列出目錄內(nèi)容的函數(shù)代碼
PHP循環(huán)列出目錄內(nèi)容代碼,一個(gè)功能函數(shù)。2010-05-05php實(shí)現(xiàn)遍歷多維數(shù)組的方法
這篇文章主要介紹了php實(shí)現(xiàn)遍歷多維數(shù)組的方法,涉及php針對(duì)多維數(shù)組的遍歷與遞歸操作實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11PHP中spl_autoload_register()函數(shù)用法實(shí)例詳解
這篇文章主要介紹了PHP中spl_autoload_register()函數(shù)用法,結(jié)合實(shí)例形式分析了__autoload函數(shù)及spl_autoload_register函數(shù)的相關(guān)使用技巧,需要的朋友可以參考下2016-07-07php簡(jiǎn)單實(shí)現(xiàn)批量上傳圖片的方法
這篇文章主要介紹了php簡(jiǎn)單實(shí)現(xiàn)批量上傳圖片的方法,實(shí)例分析了php文件傳輸?shù)膶?shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-05-05