单态
1 <?php 2 //单例 3 class Db{ 4 //创建静态私有的变量保存该类对象 5 static private $instance; 6 //参数 7 static private $config; 8 //防止直接创建对象 9 private function __construct($config){ 10 self::$config = $config; 11 echo 'new obj'; 12 } 13 //防止克隆对象 14 private function __clone(){ 15 return false; 16 } 17 static public function getInstance($config){ 18 //判断$instance是否是Uni的对象 19 if (!self::$instance instanceof self) { 20 //没有则创建 21 self::$instance = new static($config); 22 } 23 return self::$instance; 24 25 } 26 public function getConfig(){ 27 return $this -> config; 28 } 29 } 30 $data = Db::getInstance(1); 31 var_dump($data -> getConfig()); 32 ?>
继续阅读与本文标签相同的文章
-
Kubernetes监控实践(1):K8s的工作原理与监控实践
2026-05-18栏目: 教程
-
奇葩设计师贾伟打开你的想象力经济
2026-05-18栏目: 教程
-
如何组建网络空间安全大赛
2026-05-18栏目: 教程
-
Jmeter四大随机数(一):$__Random(,,)
2026-05-18栏目: 教程
-
从零开始入门 K8s| 阿里技术专家详解 K8s 核心概念
2026-05-18栏目: 教程
