当前位置: 移动技术网 > IT编程>开发语言>PHP > php设计模式 Singleton(单例模式)

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

2019年04月21日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

<?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));

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网