用php实现像JSP,ASP里Application那样的全局变量
2015-01-24信息快讯网
<?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 at(@)符号的用法简介
php ignore_user_abort与register_shutdown_function 使用方法
PHP加速 eAccelerator配置和使用指南
PHP5.2中date()函数显示时间与北京时间相差8小时的解决办法
php session 错误
PHP session有效期问题
PHP mb_convert_encoding 获取字符串编码类型实现代码
Uchome1.2 1.5 代码学习 common.php
对squid中refresh_pattern的一些理解和建议
php Http_Template_IT类库进行模板替换
php session应用实例 登录验证
php session处理的定制
PHP setcookie() cannot modify header information 的解决方法
Zend Studio for Eclipse的java.lang.NullPointerException错误的解决方法
Cannot modify header information错误解决方法
smarty section简介与用法分析
自动分页的不完整解决方案
新手配置 PHP 调试环境(IIS+PHP+MYSQL)
require(),include(),require_once()和include_once()的异同
PHP中通过ADODB库实现调用Access数据库之修正版本 原创
php基础知识:类与对象(5) static
function.inc.php超越php
用PHP函数解决SQL injection
Apache2 httpd.conf 中文版
弄了个检测传输的参数是否为数字的Function
PHP date函数参数详解
利用static实现表格的颜色隔行显示
PHP 和 XML: 使用expat函数(三)
PHP 和 XML: 使用expat函数(二)
使用数据库保存session的方法
在PHP3中实现SESSION的功能(一)
vBulletin Forum 2.3.xx SQL Injection
用PHP函数解决SQL injection