php单例模式
<?php
class Single{
private $name;//声明一个私有的实例变量
//防止直接创建对象
private function __construct(){
echo "实例化了....";
}
//防止克隆
private function __clone(){
}
static public $instance;
static public function getinstance(){
if(!self::$instance) self::$instance = new self();
return self::$instance;
}
public function setname($n){ $this->name = $n; }
public function getname(){ return $this->name; }
}
$oa = Single::getinstance();
$oa->setname('hello world');
echo $oa->getname();//good morning
?>