单例模式:3私1公2静态
<?php /** * 3私1公2静态 */ class Db { protected static $ins = null; protected function __construct() { } public static function getInstance() { if (self::$ins === null) { self::$ins = new self(); } return self::$ins; } protected function __clone() { } } $a = Db::getInstance(); $b = Db::getInstance(); $c = new StdClass(); $d = new StdClass(); if ($a === $b) { echo '相等'; // 相等 } if ($c === $d) { echo '相等'; // 不相等 } ?>