Home >Backend Development >PHP Tutorial >application error Use php to implement global variables like Application in JSP and ASP
复制代码 代码如下:
/**
* Function: Realize global variables like Application in JSP and ASP
* author: [url]www.itzg.net[/url]
* version: 1.0
* Copyright: Please keep the copyright statement when reprinting like this
*/
/*+----------------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
{
/**File to save shared variables*/
var $save_file = 'Application/Application';
/**The name of the shared variable*/
var $application = null;
/**Serialized data*/
var $app_data = '';
/**Have you done setValue operation to prevent frequent file writing operations?*/
var $__writed = false;
/**
*Constructor
*/
function Application()
{
$this->application = array();
}
/**
* Set global variables
* @param string $var_name The variable name to be added to the global variable
* @param string $var_value The value of the variable
*/
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();
}
/**
* Get the value stored in the global variable
* @return array
*/
function getValue()
{
if (!is_file($this->save_file))
$this->__writeToFile();
return @unserialize(@file_get_contents($this->save_file));
}
/**
* Write serialized data to file
* @scope private
*/
function __writeToFile()
{
$fp = @fopen($this->save_file,"w");
@fwrite($fp,$this->app_data);
@fclose($fp);
}
}
?>
以上就介绍了application error 用php实现像JSP,ASP里Application那样的全局变量,包括了application error方面的内容,希望对PHP教程有兴趣的朋友有所帮助。