Using PHP to implement global variable _php techniques like jsp,asp application

Source: Internet
Author: User
Copy Code code as follows:


<?php
/**
* Function: Implement global variables like application in jsp,asp
* Author: [Url]www.itzg.net[/url]
* version:1.0
* Copyright: Please keep the copyright notice in this reprint.
*/
/*+----------------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 shared variable's file * *
var $save _file = ' application/application ';
/** the name of the shared variable * *
var $application = null;
/** Data after serialization * *
var $app _data = ';
/** has done setvalue operations to prevent frequent file-writing operations * *
var $__writed = false;

/**
* Constructor
*/
function Application ()
{
$this->application = Array ();
}
/**
* Set Global variables
* @param string $var _name The name of the variable 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 ();
}
/**
* Gets 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);
}
}

?>

Related Article

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.