phpでJSP,ASPのアプリケーションのようなグローバル変数を実現する
2501 ワード
/**
* : 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);
}
}
?>