[php]php设计模式 Singleton(单例模式)

1 <?php
2 /**
3 * 单例模式
4 *
5 * 保证一个类仅有一个实例,并提供一个访问它的全局访问点
6 *
7 */
8 class Singleton
9 {
10 staticprivate$_instance=null;
11
12 privatefunction __construct()
13 {
14 }
15
16 staticpublicfunction getInstance()
17 {
18 if(is_null(self::$_instance)) {
19 self::$_instance=new Singleton();
20 }
21 return self::$_instance;
22 }
23
24 publicfunction display()
25 {
26 echo"it is a singlton class function";
27 }
28 }
29
30 // $obj = new Singleton(); // 声明不能成功
31 $obj= Singleton::getInstance();
32 var_dump($obj);
33 $obj->display();
34
35 $obj1= Singleton::getInstance();
36 var_dump(($obj===$obj1));

posted on 2011-06-15 20:41  bluefrog  阅读(6137)  评论(3编辑  收藏  举报