单例模式又称为职责模式,它用来在程序中创建一个单一功能的访问点,通俗地说就是实例化出来的对象是唯一的。
所有的单例模式至少拥有以下三种公共元素:
1. 它们必须拥有一个构造函数,并且必须被标记为private
2. 它们拥有一个保存类的实例的静态成员变量
3. 它们拥有一个访问这个实例的公共的静态方法
单例类不能再其它类中直接实例化,只能被其自身实例化。它不会创建实例副本,而是会向单例类内部存储的实例返回一个引用。

一个简单的例子:

class DB{ private $_link; // 保持单例类的静态变量 static $_instance; // 私有的构造函数 private function construct(){ $this->_link = @mysqli_connect(HOST, USER, PASSWORD, DATABASE); if(! ($this->_link)){ echo 'Something wrong occurs on the database connection!'; } } // 防止单例类被克隆 private function clone(){} // 外界访问单例类实例的接口 public static function getInstance(){ if(! (self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } }

PS:以上定义的一个 clone() 函数,防止单例类对象被克隆。

以下也是一个简单的数据库操作类的单例,供参考:

class DB { /** * the database connection * @var resource * @access private */ private $_link; /** * the static instance of single db * @var object * @access static */ static $_instance; /** * construct the single object * @return null * @access private */ private function construct(){ $this->_link = @mysqli_connect(HOST, USER, PASSWORD, DATABASE); if(! ($this->_link)){ echo 'Something wrong occurs on the database connection!'; } } /** * empty clone * @return null * @access private */ private function clone(){} /** * for other object to get the instance of db * @return self::instance * @access public */ public static function getInstance(){ if(! (self::$_instance instanceof self)){ self::$_instance = new self(); } return self::$_instance; } /** * query * @param sql string * @param message string * @return resource * @access public */ public function query($sql,$message){ $result = @mysqli_query($this->$_link, $sql) or die($message . mysqli_error($this->$_link)); return $result; } /** * mysqli_num_rows * @param result resource * @return int * @access public */ public function num($result){ return @mysqli_num_rows($result); } /** * mysqli_fetch_array * @param result resource * @return array * @access public */ public function fetchArr($result){ return @mysqli_fetch_array($result); } /** * mysqli_insert_id * @return int * @access public */ public function last_id(){ return @mysqli_insert_id($this->_link); } /** * close the database connection * @param result resource * @return null * @access public */ public function close(){ @mysqli_close($this->_link); } /** * fetch once result from the specific sql query * @param sql string * @param message string * @return array * @access public */ public function fetchArrOnce($sql, $message){ $result = $this->query($sql, $message); $row = $this->fetchArr($result); return $row; } /** * fetch all results from the specific sql query * @param sql string * @param message string * @return array * @access public */ public function fetchArrMore($sql, $message){ $result = $this->query($sql, $message); $moreRow = array(); while($row = $this->fetchArr($result)){ $moreRow[] = $row; } return $moreRow; } /** * fetch the number of results from the specific sql query * @param sql string * @param message string * @return array * @access public */ public function fetchNum($sql, $message){ $result = $this->query($sql, $message); $resultNum = $this->num($result); return $resultNum; } /** * mysqli_prepare * @param sql string * @return stmt object * @access public */ public function prepare($sql){ return @mysqli_prepare($this->_link, $sql); } /** * mysqli_stmt_execute * @param stmt object * @param message string * @return bool * @access public */ public function stmt_execute($stmt, $message){ @mysqli_stmt_execute($stmt) or die($message . mysqli_error($this->_link)); } }

使用:

define("HOST", "localhost"); define("USER", "root"); define("PASSWORD", ""); define("DATABASE", "eee"); $db = DB::getInstance();

以上就是php 单例模式的定义与使用的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码