标签:
1 <?php 2 3 error_reporting(E_ALL | E_STRICT); 4 5 class single{ 6 7 public $hash; 8 9 static protected $ins = null; 10 11 final protected function __construct(){ //final 能被继承,不能被重写(防止子类重写构造方法) 12 $this->hash = mt_rand(1,9999); 13 } 14 15 static public function getInstance(){ 16 if(self::$ins instanceof self){ //instanceof 判断某个对象是不是某个类的实例 17 return self::$ins; 18 } 19 self::$ins = new self(); 20 return self::$ins; 21 } 22 } 23 24 $s1 = single::getInstance(); 25 $s2 = single::getInstance(); 26 27 var_dump($s1); 28 var_dump($s2); 29 30 //son类继承single类 31 class son extends single{ 32 } 33 34 $s1 = son::getInstance(); 35 $s2 = son::getInstance(); 36 37 var_dump($s1); 38 var_dump($s2);
页面输出
object(single)[1] public ‘hash‘ => int 9762 object(single)[1] public ‘hash‘ => int 9762 object(single)[1] public ‘hash‘ => int 9762 object(single)[1] public ‘hash‘ => int 9762
标签:
原文地址:http://www.cnblogs.com/dee0912/p/4306071.html