Copy codeThe Code is as follows:
<? Php
/**
* Function: implement global variables such as JSP and ASP Application
* Author: [url] www.itzg.net [/url]
* Version: 1.0
* Copyright: reprinted in this way. Please retain the copyright statement.
*/
/* + ---------------- 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 the file with shared variables */
Var $ save_file = 'application/application ';
/** Name of the shared variable */
Var $ application = null;
/** Serialized data */
Var $ app_data = '';
/** Whether setValue operations have been performed to prevent frequent file write operations */
Var $ __writed = false;
/**
* Constructor
*/
Function Application ()
{
$ This-> application = array ();
}
/**
* Set global variables
* @ Param string $ var_name name of the variable to be added to the global variable
* @ Param string $ var_value variable 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 ();
}
/**
* Obtain 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 a file
* @ Scope private
*/
Function _ writeToFile ()
{
$ Fp = @ fopen ($ this-> save_file, "w ");
@ Fwrite ($ fp, $ this-> app_data );
@ Fclose ($ fp );
}
}
?>