<?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));
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
闲下来想一想
自从去年系统大升级以来,不但需要维稳新系统,新需求一个接一个。虽然疫情期间,工作不但没有闲下来反而更忙了。不过话...
[阅读全文]
-
-
-
-
-
-
网友评论