用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);
 }
}

?> 

相关文章

PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍

PHP5全版本绕过open_basedir读文件脚本漏洞详细介绍

漏洞很久之前(大概5年前)被提出来了,但并不是php代码上的问题,所以问题一直存在,直到现在。我一直没留意,后来yaseng告诉我的,他测试了好像5.5都可以。 漏洞详情在这里 http...

php如何获取文件的扩展名

php如何获取文件的扩展名

网上也有很多类似的方法,不过都存在这样那样的不严谨的问题,本文就不一一分析了,这里只给出最正确的利用php 获取文件扩展名(文件后缀名)的方法。 function get_exte...

两款万能的php分页类

本文为大家分享个超级好用、万能的php分页类,具体的实现代码如下 第一款php分页类 <?php /* * To change this template, choo...

preg_match_all使用心得分享

preg_match_all — 进行全局正则表达式匹配 说明 复制代码 代码如下:int preg_match_all ( string pattern, string subject...

phpmailer发送邮件之后,返回收件人是否阅读了邮件的方法

很多人在使用phpmailer发送邮件之后,都想知道对方是否阅读了邮件?通常来说,这个我们是无法知道的,那么有没有办法呢? 对于这个问题有一个简单的解决方法,我们知道,邮件内容可以以ht...