1 <?php 2 3 //单例模式 4 class Database{ 5 private $_db; 6 public static $_instance; 7 8 //private的构造函数,防止被实例化 9 private function __construct(){ 10 $this->__db = pg_connect('dbname=test'); 11 } 12 13 public static function getInstance(){ 14 if(!(self::$_instance instanceof self)){ 15 self::$_instance = new self(); 16 } 17 return self::$_instance; 18 } 19 20 //private的clone方法,防止对象clone后产生多个实例对象 21 private function __clone(){ 22 trigger_error('Clone is not allow' ,E_USER_ERROR); 23 } 24 25 public function query($sql){ 26 return pg_query($this->$_db,$sql); 27 } 28 } 29 30 //调用方法 31 $conn = Database::getInstance(); 32 $conn->query(' select * from user ');