首页 > Web开发 > 详细

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

时间:2016-01-31 12:51:03      阅读:105      评论:0      收藏:0      [点我收藏+]
复制代码 代码如下:

<?php
/**
* 单例模式
*
* 保证一个类仅有一个实例,并提供一个访问它的全局访问点
*
*/
class Singleton
{
static private $_instance = null;

private function __construct()
{
}

static public function getInstance()
{
if(is_null(self::$_instance)) {
self::$_instance = new Singleton();
}
return self::$_instance;
}

public function display()
{
echo "it is a singlton class function";
}
}

// $obj = new Singleton(); // 声明不能成功
$obj = Singleton::getInstance();
var_dump($obj);
$obj->display();

$obj1 = Singleton::getInstance();
var_dump(($obj === $obj1));

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

原文:http://www.jb51.net/article/27481.htm

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!