Copy CodeThe code is as follows:
/**
* Function: Implement global variables like application in jsp,asp
* Author: [Url]www.itzg.net[/url]
* version:1.0
* Copyright: So reproduced please retain the copyright notice
*/
/*+----------------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
{
/** save a file for shared variables */
var $save _file = ' application/application ';
/** name of shared variable */
var $application = null;
/** data after serialization */
var $app _data = ";
/** have done setvalue operations to prevent frequent write file operations */
var $__writed = false;
/**
* Constructor function
*/
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 The value of the _value 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 values 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);
}
}
?>
The above describes the application error in PHP implementation like jsp,asp application such a global variable, including the application error content, I hope the PHP tutorial interested in a friend helpful.