Application error using PHP to implement global variables like application in jsp,asp

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.