Use PHP to implement global variables like Application in JSP and ASP_PHP Tutorial

WBOY
Release: 2016-07-21 15:58:19
Original
1006 people have browsed it

复制代码 代码如下:


/**
* Function: implement global variables like Application in JSP and ASP
* author: [url]www.itzg.net[/url]
* version: 1.0
* Copyright: reproduced as shown Please keep 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
{
 /**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);
 }
}

?> 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/317609.htmlTechArticle复制代码 代码如下: ?php /** *功能:实现像JSP,ASP里Application那样的全局变量 *author:[url]www.itzg.net[/url] *version:1.0 *版权:如许转载请保留版权声...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template