PHP编程_单例模式

管理员 发布于 6年前   335
//单列
class DL {
	static protected $ins = null; 
	protected $sj;  //存唯一随机数 测试用

	final protected function __construct(){
		$this->sj = rand(1,10000); //生成随机数
		echo $this->sj;  //测试
	}

	final protected function __clone(){

	} 
        //唯一开放的方法
	static public function getIns(){
		if(self::$ins instanceof self){
		  return self::$ins; 
		}
		  self::$ins = new self();
		  return self::$ins;
	}
}

//测试效果 (图我就不贴了,有兴趣的可以自己试一下)
$a =  DL::getIns();
print_r($a) ;


请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

该博客于2020-12-7日,后端基于go语言的beego框架开发
前端页面使用Bootstrap可视化布局系统自动生成

是我仿的原来我的TP5框架写的博客,比较粗糙,底下是入口
侯体宗的博客

      订阅博客周刊

文章标签

友情链接

HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群
侯体宗的博客