php 单例模式
<?php class DB { private static $dh = null; private function __construct() { self::$dh = mysqli_connect("localhost","root",""); if (!self::$dh) { die('Could not connect: ' . mysql_error()); } echo 'mysql connected...'.PHP_EOL; } static public function getInstance() { if (is_null(self::$dh)) { self::$dh = new self(); } return self::$dh; } public function select() { echo 'selecting data...'.PHP_EOL; } } $db1 = DB::getInstance(); $db2 = DB::getInstance(); var_dump($db1); var_dump($db1 === $db2); /* mysql connected... object(DB)#1 (0) { } bool(true) */