用php实现像JSP,ASP里Application那样的全局变量

yipeiwu_com6年前PHP代码库
复制代码 代码如下:


<?php
/**
 * 功能:实现像JSP,ASP里Application那样的全局变量
 * author: [url]www.itzg.net[/url]
 * version: 1.0
 * 版权:如许转载请保留版权声明
 */
/*+----------------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
{
 /**保存共享变量的文件*/
 var $save_file    = 'Application/Application';
 /**共享变量的名称*/
 var $application  = null;
 /**序列化之后的数据*/
  var $app_data    = '';
  /**是否已经做过setValue的操作 防止频繁写文件操作*/
  var $__writed    = false;

  /**
   * 构造函数
   */
  function Application()
  {
   $this->application = array();
  }
  /**
   * 设置全局变量
   * @param string $var_name 要加入到全局变量的变量名
   * @param string $var_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();
  }
 /**
  * 取得保存在全局变量里的值
  * @return array
  */
 function getValue()
 {
     if (!is_file($this->save_file))
         $this->__writeToFile();
     return @unserialize(@file_get_contents($this->save_file));
 }
 /**
  * 写序列化后的数据到文件
  * @scope private
  */
 function __writeToFile()
 {
  $fp = @fopen($this->save_file,"w");
  @fwrite($fp,$this->app_data);
  @fclose($fp);
 }
}

?> 

相关文章

PHP自动识别当前使用移动终端

虽然现在利用jquery可以准确地判断出当前使用的是那种客户端,但是有时候根据功能和需求,我们可能需要用php来判断当前程序运行的环境,jquery在这里就不说了,这里直接讲讲php是怎...

[PHP]实用函数2

1、产生随机字符串函数 复制代码 代码如下:function random($length) {  $hash = @#@#; ...

php 上传功能实例代码

1.上传表单 upload.html 复制代码 代码如下: <form enctype="multipart/form-data" action="upload.php" meth...

解析php dirname()与__FILE__常量的应用

__FILE__表示当前所在文件的绝对路径包括文件名,dirname(__FILE__)表示当前文件的绝对路径,basename(__FILE__)表示当前文件的文件名称,dirname...

php简单实现查询数据库返回json数据

示例代码一: // 设置返回json格式数据 header('content-type:application/json;charset=utf8'); //连接数据库 $link...