摘要:<?phpclass Hubby{ private function __construct(){} private function __clone(){} protected static $instance=null; public static function getInstance() { if (is_null(static::$instance)){ static::$
<?php
class Hubby
{
private function __construct(){}
private function __clone(){}
protected static $instance=null;
public static function getInstance()
{
if (is_null(static::$instance)){
static::$instance=new static();
}
return static::$instance;
}
}
$hubby1=Hubby::getInstance();
$hubby2=Hubby::getInstance();
echo( $hubby1 instanceof hubby)?'是':'不是';
echo '<br>';
echo( $hubby2 instanceof hubby)?'是':'不是';
echo '<br>';
echo($hubby1===$hubby2)?'完全相等':'不相等';
echo '<br>';
var_dump($hubby1,$hubby2);
批改老师:天蓬老师批改时间:2019-06-13 17:07:25
老师总结:单例模式的应用场景 还是很多...
很多类,为了效率, 不允许多重实例化