abstract:<?php class Hubby { private function __construct(){} private function __clone(){} protected static $instance = null;
<?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;
}
}
// 從外部來實(shí)例化Hubby類
$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);
Correcting teacher:天蓬老師Correction time:2019-01-10 16:40:55
Teacher's summary:js是天生的單例模式, 不過,單例還是很有用的, 不是嗎? php, java都有