手机
当前位置:查字典教程网 >编程开发 >php教程 >用php实现像JSP,ASP里Application那样的全局变量
用php实现像JSP,ASP里Application那样的全局变量
摘要:复制代码代码如下:

复制代码 代码如下:

<?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=newApplication();

$a->setValue("t1","arui");

$a->setValue("arr",$arr);

$u=$a->getValue();

---------------------------------------------+*/

classApplication

{

/**保存共享变量的文件*/

var$save_file='Application/Application';

/**共享变量的名称*/

var$application=null;

/**序列化之后的数据*/

var$app_data='';

/**是否已经做过setValue的操作防止频繁写文件操作*/

var$__writed=false;

/**

*构造函数

*/

functionApplication()

{

$this->application=array();

}

/**

*设置全局变量

*@paramstring$var_name要加入到全局变量的变量名

*@paramstring$var_value变量的值

*/

functionsetValue($var_name,$var_value)

{

if(!is_string($var_name)||empty($var_name))

returnfalse;

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();

}

/**

*取得保存在全局变量里的值

*@returnarray

*/

functiongetValue()

{

if(!is_file($this->save_file))

$this->__writeToFile();

return@unserialize(@file_get_contents($this->save_file));

}

/**

*写序列化后的数据到文件

*@scopeprivate

*/

function__writeToFile()

{

$fp=@fopen($this->save_file,"w");

@fwrite($fp,$this->app_data);

@fclose($fp);

}

}

?>

【用php实现像JSP,ASP里Application那样的全局变量】相关文章:

多php服务器实现多session并发运行

用php写的serv-u的web申请账号的程序

使用函数递归实现基于php和MySQL的动态树型菜单

php简单实现快速排序的方法

php使用GD实现颜色渐变实例

使用php的swoole实现高效安全的ftp服务器

用PHP实现WEB动态网页静态

php实现递归抓取网页类实例

PHP实现的增强性mhash函数

php实现ping

精品推荐
分类导航