<?php
class Single{
public static $instance = null;
//防止子类扩大权限,使外部可以new对象
final protected function __construct(){
}
public static function getInstance(){
if(self::$instance === null){
self::$instance = new self();
}
return self::$instance;
}
//防止clone
final protected function __clone(){
}
}
$s1 = Single::getInstance();
$s2 = Single::getInstance();
if($s1 === $s2){
echo "s1和s2是一个对象";
}else{
echo "s1和s2不是一个对象";
}
时间: 2024-11-06 17:19:16