<?php
/**
*单列模式
*/
class Dome
{
private static $instance = null;
//把构造方法私有化
public function __construct()
{
}
//禁止克隆对象
private function __clone()
{
}
//生成当前类的唯一实例
public static function getInstance()
{
//判断当前属性$instance是不是当前类的对象
if (!self::$instance instanceof self)
{
self::$instance = new self();
}
//如果是当前类的对象
return self::$instance;
}
}
$obj1 = Dome::getInstance();
$obj2 = Dome::getInstance();
$obj3 = Dome::getInstance();
$obj4 = Dome::getInstance();
var_dump($obj1,$obj2,$obj3,$obj4);生成的值 只会产生一个id值
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号