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

简介:这是php设计模式 Singleton(单例模式)的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=338757' scrolling='no'>
1 <?php
2 /**
3 * 单例模式
4 *
5 * 保证一个类仅有一个实例,并提供一个访问它的全局访问点
6 *
7 */
8 class Singleton
9 {
10 static private $_instance = null;
11
12 private function __construct()
13 {
14 }
15
16 static public function getInstance()
17 {
18 if(is_null(self::$_instance)) {
19 self::$_instance = new Singleton();
20 }
21 return self::$_instance;
22 }
23
24 public function 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));

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/338757.html pageNo:8
原文地址:https://www.cnblogs.com/ooooo/p/2246214.html