<?php
class Dog {
private function __construct () {
}
static private $instance;
static public function getInstance () {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
}
$dog1 = Dog::getInstance();
$dog2 = Dog::getInstance();
if ($dog1 === $dog2) {
echo "这是同一个对象";
} else {
echo "这是两个不同的对象";
}
php高级实战-单例模式
最新推荐文章于 2024-06-14 09:22:46 发布
![](https://img-home.csdnimg.cn/images/20240611030827.png)