background image

$c

 = Hello::getInstance('World'); 

// $a === $c 

 

$d

 = GoodBye::getInstance(); 

$e

 = GoodBye::getInstance(); 

// $d === $e 

 

$f

 = GoodBye::getInstance('your'); 

// $d !== $f 

?>
可以看到 PHP 多例模式需要 getInstance()传递关键值,对于给定的关键值,PHP 多例模式
只会存在唯一的对象实例,PHP 多例模式节省内存,确保同一个对象的多个实例不发生
冲突。