protected function initLink() { // $options = array( // 'host' =>'127.0.0.1', // 'port' => '3306', // 'user' => 'root', // 'pass' =>'root', // 'charset' => 'utf8', // 'dbname' => 'demo_shop' // ); $this->db = MySQLDB::getInstance($GLOBALS['config']['database']); }
<?php class MySQLDB { private static $instance; private function __construct($p1) { } public static function getInstance($p1) { if (!self::$instance instanceof self) { self::$instance = new self($p1); } return self::$instance; } private function __clone() { } } $db1 = MySQLDB::getInstance($a); $db2 = MySQLDB::getInstance($a); var_dump($db1, $db2);
public function sess_open() { $this->db = MySQLDB::getInstance($GLOBALS['config']['database']); }