额,只是复习到这里,做点笔记吧。
单例模式。何谓也?我想就是唯一吧。唯一的意思大概希特勒已经说的很清楚了。就是我也说不明白……把代码贴上来了事。
1 <?php 2 // Single instance mode(Pardon me for my Chinglish...if there is someone look this...) 3 class Single { 4 // the private construct method for not to be extends or get instance 5 private function __construct() 6 { 7 // in this moment just do nothing 8 } 9 10 private static $instance = null; // for store the single instance 11 12 // private function __clone(){} # Is it necessary? 13 14 public static function getObj() 15 { 16 if ( ! isset(self::$instance) ) 17 { 18 $obj = new self(); 19 20 return self::$instance = $obj; 21 } 22 else 23 return self::$instance; 24 } 25 } 26 27 // $single = new Single(); 28 29 $single = Single::getObj(); 30 $singl1 = Single::getObj(); 31 $singl2 = Single::getObj(); 32 33 var_dump($single, $singl1, $singl2); 34 35 if ($single === $singl1) 36 echo '<pre>So...Like this, is it right? ';
以前听培训老师讲的是“三私一公”,可这个三思,我也忘了,不记得写__clone了。也茫然到底写还是不写。为此我搜了两条记录。只是看看……
http://blog.csdn.net/shenpengchao/article/details/51865083
http://www.jb51.net/article/68752.htm
End of jottings.