<?php
class Preferences{
private $props=array();
private static $instance;
private function __construct(){}
public static function getInstance(){
if(empty(self::$instance)){
self::$instance=new Preferences();
}
return self::$instance;
}
public function setProperty($key,$val){
$this->props[$key]=$val;
}
public function getProperty($key){
return $this->props[$key];
}
}
$result=Preferences::getInstance();
$result->setProperty('user','xiaowei');
echo $result->getProperty('user');
unset($result);
$pref=Preferences::getInstance();
print $pref->getProperty('user');
?>
php单例模式,可用于全局变量
最新推荐文章于 2023-10-25 17:22:41 发布