用php实现像JSP,ASP里Application那样的全局变量
程序员文章站
2024-02-01 20:42:16
复制代码 代码如下:
<?php
/**
* 功能:实现像jsp,asp里application那样的全局变量
* author: [url]www.itzg.net[/url]
* version: 1.0
* 版权:如许转载请保留版权声明
*/
/*+----------------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;
/**序列化之后的数据*/
var $app_data = '';
/**是否已经做过setvalue的操作 防止频繁写文件操作*/
var $__writed = false;
/**
* 构造函数
*/
function application()
{
$this->application = array();
}
/**
* 设置全局变量
* @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));
}
/**
* 写序列化后的数据到文件
* @scope private
*/
function __writetofile()
{
$fp = @fopen($this->save_file,"w");
@fwrite($fp,$this->app_data);
@fclose($fp);
}
}
?>
复制代码 代码如下:
<?php
/**
* 功能:实现像jsp,asp里application那样的全局变量
* author: [url]www.itzg.net[/url]
* version: 1.0
* 版权:如许转载请保留版权声明
*/
/*+----------------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;
/**序列化之后的数据*/
var $app_data = '';
/**是否已经做过setvalue的操作 防止频繁写文件操作*/
var $__writed = false;
/**
* 构造函数
*/
function application()
{
$this->application = array();
}
/**
* 设置全局变量
* @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));
}
/**
* 写序列化后的数据到文件
* @scope private
*/
function __writetofile()
{
$fp = @fopen($this->save_file,"w");
@fwrite($fp,$this->app_data);
@fclose($fp);
}
}
?>
上一篇: JQuery介绍