用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 命令行工具 shell_exec, exec, passthru, system详细使用介绍

PHP 命令行工具 shell_exec, exec, passthru, system详细使用介绍

所有这些命令都衍生一个子进程,用于运行您指定的命令或脚本,并且每个子进程会在命令输出写到标准输出 (stdout) 时捕捉它们。 shell_exec() shell_exec() 命令...

解析 thinkphp 框架中的部分方法

 1 thinkphp 框架 中判断输入的数值和数据库中的数值是否一致    首先 需要在view文件夹下建一个模板 名为zhuce.html <...

WordPress的主题编写中获取头部模板和底部模板

get_header()(获取头部) 引入主题的头部模板,默认会引入当前主题目录里的 header.php 文件。如果指定了一个名称,则引入当前主题目录的 header-{name}.p...

解析如何去掉CodeIgniter URL中的index.php

CI默认的rewrite url中是类似这样的,例如你的CI根目录是在/CodeIgniter/下,你的下面的二级url就类似这样http://localhost/CodeIgniter...

php异常处理捕获错误整理

PHP的错误报告有三种: 1、错误,语法解析错误,致命错误 2、警告 3、注意 后果: 错误 -> 致命错误,会终止已下程序的执行,语法错误的话,PHP压根就没执行。 警告 -&g...