<?php
class single{
private static $instance;
// 设置成private为了防止直接new
private function __construct(){
echo 'this is construct'."<br>";
}
public static function getinstance(){
if(!self::$instance instanceof self){
self::$instance=new self;
}
return self::$instance;
}
public function __clone(){
trigger_error('clone is not allow');// 为了防止克隆,也可以直接把此方法写成private
}
public function test(){
echo 'test :do it';
}
}
$a=new single();//Fatal error: Call to private single::__construct() fro
// single::test();//ini_set('display_errors',0);
// single::getinstance();
$a=single::getinstance();
$a->test();
$clone = clone $a; // Notice: clone is not allow in
php单例模式
最新推荐文章于 2015-07-03 11:33:49 发布