首頁  >  文章  >  後端開發  >  php實作單例模式的方法及意義

php實作單例模式的方法及意義

墨辰丷
墨辰丷原創
2018-06-02 11:19:441283瀏覽

這篇文章主要介紹了php實作單例模式的方法,告訴大家為什麼要用單例模式,有興趣的朋友可以參考一下

一、什麼是單例模式? 1、意義      作為物件的建立模式,單例模式確保某一個類別只有一個實例,並且自行實例化並且為整個系統全域提供這個實例。它不會建立實例副本,而是會向單例類別內部儲存的實例傳回一個參考。
2、單例模式的三個重點:(1). 需要一個唯一保存類別的實例的靜態成員變數:
private static $_instance;   
(2).建構函式和複製函式必須宣告為私有的,防止外部程式new類別而失去單例模式的意義:

private function __construct()  
{  
  $this->_db = pg_connect('xxxx'); 
}  
private function __clone() 
{ 
}//覆盖__clone()方法,禁止克隆

(3).必須提供一個存取這個實例的公用的靜態方法(通常為getInstance方法),從而傳回唯一實例的一個引用

public static function getInstance()  
{  
  if(! (self::$_instance instanceof self) )  
  {  
    self::$_instance = new self();  
  } 
  return self::$_instance;  
 
}

#二、為什麼要使用單例模式?
1、PHP缺點:
        
        PHP語言是指解釋型的腳本語言,此操作機制使得每個PHP頁面都被解釋執行後,所有相關的資源都會解釋被回收。也就是說,PHP在語言層級上沒有辦法讓某個物件常駐內存,這和asp.net、Java等編譯型是不同的,例如在Java中單例會一直存在於整個應用程式的生命週期裡,變數是跨頁面層級的,真正可以做到這個實例在應用程式生命週期中的唯一性。然而在PHP中,所有的變數無論是全域變數或類別的靜態成員,都是頁面層級的,每次頁面被執行時,都會重新建立新的對象,都會在頁面執行完畢後被清空,這樣似乎PHP單例模式就沒有什麼意義了,所以PHP單例模式我覺得只是針對單次頁面級請求時出現多個應用場景並需要共享同一對象資源時是非常有意義的。

2、單例模式在PHP中的應用場合:#(1)、應用程式與資料庫互動
  一個應用程式中會存在大量的資料庫操作,例如過資料庫句柄來連接資料庫這一行為,使用單例模式可以避免大量的new操作,因為每一次new操作都會消耗記憶體資源和系統資源。
(2)、控製設定資訊
 如果系統中需要有一個類別來全域控制某些設定資訊, 那麼使用單例模式可以很方便的實作.

三、如何實現單例模式? 1、普通的資料庫存取範例:

<?php 
...... 
//初始化一个数据库句柄 
$db = new DB(...); 
 
//添加用户信息 
$db->addUserInfo(...); 
 
...... 
 
//在函数中访问数据库,查找用户信息 
function getUserInfo() 
{ 
  $db = new DB(...);//再次new 数据库类,和数据库建立连接 
  $db = query(....);//根据查询语句访问数据库 
} 
 
?>

2、應用程式單例模式對資料庫進行操作:

<?php 
class DB  
{  
  private $_db;  
  private static $_instance;  
  
  private function __construct(...)  
  {  
    $this->_db = pg_connect(...);//postgrsql  
  }  
  
  private function __clone() {}; //覆盖__clone()方法,禁止克隆  
  
  public static function getInstance()  
  {  
    if(! (self::$_instance instanceof self) ) {  
      self::$_instance = new self();  
    }  
    return self::$_instance;  
  }  
  
  public function addUserInfo(...) 
  { 
  } 
   public function getUserInfo(...) 
  {  
  } 
 
} 
 
//test  
$db = DB::getInstance();  
$db->addUserInfo(...);  
$db->getUserInfo(...);  
 
?>

 下面的程式碼是PDO操作資料庫類別的封裝,採用了單例模式:

<?php
/**
 * MyPDO
 */
class MyPDO
{
  protected static $_instance = null;
  protected $dbName = &#39;&#39;;
  protected $dsn;
  protected $dbh;
  
  /**
   * 构造
   * 
   * @return MyPDO
   */
  private function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
  {
    try {
      $this->dsn = &#39;mysql:host=&#39;.$dbHost.&#39;;dbname=&#39;.$dbName;
      $this->dbh = new PDO($this->dsn, $dbUser, $dbPasswd);
      $this->dbh->exec(&#39;SET character_set_connection=&#39;.$dbCharset.&#39;, character_set_results=&#39;.$dbCharset.&#39;, character_set_client=binary&#39;);
    } catch (PDOException $e) {
      $this->outputError($e->getMessage());
    }
  }
  
  /**
   * 防止克隆
   * 
   */
  private function __clone() {}
  
  /**
   * Singleton instance
   * 
   * @return Object
   */
  public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset)
  {
    if (self::$_instance === null) {
      self::$_instance = new self($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
    }
    return self::$_instance;
  }
  
  /**
   * Query 查询
   *
   * @param String $strSql SQL语句
   * @param String $queryMode 查询方式(All or Row)
   * @param Boolean $debug
   * @return Array
   */
  public function query($strSql, $queryMode = &#39;All&#39;, $debug = false)
  {
    if ($debug === true) $this->debug($strSql);
    $recordset = $this->dbh->query($strSql);
    $this->getPDOError();
    if ($recordset) {
      $recordset->setFetchMode(PDO::FETCH_ASSOC);
      if ($queryMode == &#39;All&#39;) {
        $result = $recordset->fetchAll();
      } elseif ($queryMode == &#39;Row&#39;) {
        $result = $recordset->fetch();
      }
    } else {
      $result = null;
    }
    return $result;
  }
  
  /**
   * Update 更新
   *
   * @param String $table 表名
   * @param Array $arrayDataValue 字段与值
   * @param String $where 条件
   * @param Boolean $debug
   * @return Int
   */
  public function update($table, $arrayDataValue, $where = &#39;&#39;, $debug = false)
  {
    $this->checkFields($table, $arrayDataValue);
    if ($where) {
      $strSql = &#39;&#39;;
      foreach ($arrayDataValue as $key => $value) {
        $strSql .= ", `$key`=&#39;$value&#39;";
      }
      $strSql = substr($strSql, 1);
      $strSql = "UPDATE `$table` SET $strSql WHERE $where";
    } else {
      $strSql = "REPLACE INTO `$table` (`".implode(&#39;`,`&#39;, array_keys($arrayDataValue))."`) VALUES (&#39;".implode("&#39;,&#39;", $arrayDataValue)."&#39;)";
    }
    if ($debug === true) $this->debug($strSql);
    $result = $this->dbh->exec($strSql);
    $this->getPDOError();
    return $result;
  }
  
  /**
   * Insert 插入
   *
   * @param String $table 表名
   * @param Array $arrayDataValue 字段与值
   * @param Boolean $debug
   * @return Int
   */
  public function insert($table, $arrayDataValue, $debug = false)
  {
    $this->checkFields($table, $arrayDataValue);
    $strSql = "INSERT INTO `$table` (`".implode(&#39;`,`&#39;, array_keys($arrayDataValue))."`) VALUES (&#39;".implode("&#39;,&#39;", $arrayDataValue)."&#39;)";
    if ($debug === true) $this->debug($strSql);
    $result = $this->dbh->exec($strSql);
    $this->getPDOError();
    return $result;
  }
  
  /**
   * Replace 覆盖方式插入
   *
   * @param String $table 表名
   * @param Array $arrayDataValue 字段与值
   * @param Boolean $debug
   * @return Int
   */
  public function replace($table, $arrayDataValue, $debug = false)
  {
    $this->checkFields($table, $arrayDataValue);
    $strSql = "REPLACE INTO `$table`(`".implode(&#39;`,`&#39;, array_keys($arrayDataValue))."`) VALUES (&#39;".implode("&#39;,&#39;", $arrayDataValue)."&#39;)";
    if ($debug === true) $this->debug($strSql);
    $result = $this->dbh->exec($strSql);
    $this->getPDOError();
    return $result;
  }
  
  /**
   * Delete 删除
   *
   * @param String $table 表名
   * @param String $where 条件
   * @param Boolean $debug
   * @return Int
   */
  public function delete($table, $where = &#39;&#39;, $debug = false)
  {
    if ($where == &#39;&#39;) {
      $this->outputError("&#39;WHERE&#39; is Null");
    } else {
      $strSql = "DELETE FROM `$table` WHERE $where";
      if ($debug === true) $this->debug($strSql);
      $result = $this->dbh->exec($strSql);
      $this->getPDOError();
      return $result;
    }
  }
  
  /**
   * execSql 执行SQL语句
   *
   * @param String $strSql
   * @param Boolean $debug
   * @return Int
   */
  public function execSql($strSql, $debug = false)
  {
    if ($debug === true) $this->debug($strSql);
    $result = $this->dbh->exec($strSql);
    $this->getPDOError();
    return $result;
  }
  
  /**
   * 获取字段最大值
   * 
   * @param string $table 表名
   * @param string $field_name 字段名
   * @param string $where 条件
   */
  public function getMaxValue($table, $field_name, $where = &#39;&#39;, $debug = false)
  {
    $strSql = "SELECT MAX(".$field_name.") AS MAX_VALUE FROM $table";
    if ($where != &#39;&#39;) $strSql .= " WHERE $where";
    if ($debug === true) $this->debug($strSql);
    $arrTemp = $this->query($strSql, &#39;Row&#39;);
    $maxValue = $arrTemp["MAX_VALUE"];
    if ($maxValue == "" || $maxValue == null) {
      $maxValue = 0;
    }
    return $maxValue;
  }
  
  /**
   * 获取指定列的数量
   * 
   * @param string $table
   * @param string $field_name
   * @param string $where
   * @param bool $debug
   * @return int
   */
  public function getCount($table, $field_name, $where = &#39;&#39;, $debug = false)
  {
    $strSql = "SELECT COUNT($field_name) AS NUM FROM $table";
    if ($where != &#39;&#39;) $strSql .= " WHERE $where";
    if ($debug === true) $this->debug($strSql);
    $arrTemp = $this->query($strSql, &#39;Row&#39;);
    return $arrTemp[&#39;NUM&#39;];
  }
  
  /**
   * 获取表引擎
   * 
   * @param String $dbName 库名
   * @param String $tableName 表名
   * @param Boolean $debug
   * @return String
   */
  public function getTableEngine($dbName, $tableName)
  {
    $strSql = "SHOW TABLE STATUS FROM $dbName WHERE Name=&#39;".$tableName."&#39;";
    $arrayTableInfo = $this->query($strSql);
    $this->getPDOError();
    return $arrayTableInfo[0][&#39;Engine&#39;];
  }
  
  /**
   * beginTransaction 事务开始
   */
  private function beginTransaction()
  {
    $this->dbh->beginTransaction();
  }
  
  /**
   * commit 事务提交
   */
  private function commit()
  {
    $this->dbh->commit();
  }
  
  /**
   * rollback 事务回滚
   */
  private function rollback()
  {
    $this->dbh->rollback();
  }
  
  /**
   * transaction 通过事务处理多条SQL语句
   * 调用前需通过getTableEngine判断表引擎是否支持事务
   *
   * @param array $arraySql
   * @return Boolean
   */
  public function execTransaction($arraySql)
  {
    $retval = 1;
    $this->beginTransaction();
    foreach ($arraySql as $strSql) {
      if ($this->execSql($strSql) == 0) $retval = 0;
    }
    if ($retval == 0) {
      $this->rollback();
      return false;
    } else {
      $this->commit();
      return true;
    }
  }
  /**
   * checkFields 检查指定字段是否在指定数据表中存在
   *
   * @param String $table
   * @param array $arrayField
   */
  private function checkFields($table, $arrayFields)
  {
    $fields = $this->getFields($table);
    foreach ($arrayFields as $key => $value) {
      if (!in_array($key, $fields)) {
        $this->outputError("Unknown column `$key` in field list.");
      }
    }
  }
  
  /**
   * getFields 获取指定数据表中的全部字段名
   *
   * @param String $table 表名
   * @return array
   */
  private function getFields($table)
  {
    $fields = array();
    $recordset = $this->dbh->query("SHOW COLUMNS FROM $table");
    $this->getPDOError();
    $recordset->setFetchMode(PDO::FETCH_ASSOC);
    $result = $recordset->fetchAll();
    foreach ($result as $rows) {
      $fields[] = $rows[&#39;Field&#39;];
    }
    return $fields;
  }
  
  /**
   * getPDOError 捕获PDO错误信息
   */
  private function getPDOError()
  {
    if ($this->dbh->errorCode() != &#39;00000&#39;) {
      $arrayError = $this->dbh->errorInfo();
      $this->outputError($arrayError[2]);
    }
  }
  
  /**
   * debug
   * 
   * @param mixed $debuginfo
   */
  private function debug($debuginfo)
  {
    var_dump($debuginfo);
    exit();
  }
  
  /**
   * 输出错误信息
   * 
   * @param String $strErrMsg
   */
  private function outputError($strErrMsg)
  {
    throw new Exception(&#39;MySQL Error: &#39;.$strErrMsg);
  }
  
  /**
   * destruct 关闭数据库连接
   */
  public function destruct()
  {
    $this->dbh = null;
  }
}
?>

呼叫方法:

<?php
require &#39;MyPDO.class.php&#39;;
$db = MyPDO::getInstance(&#39;localhost&#39;, &#39;root&#39;, &#39;123456&#39;, &#39;test&#39;, &#39;utf8&#39;);
$db->query("select count(*) frome table");
$db->destruct();
?>

總結:以上就是本篇文的全部內容,希望對大家的學習有所幫助。

相關推薦:

php識別翻轉iphone拍攝的顛倒圖片

PHP實作登入驗證碼校驗功能

PHP產生騰訊雲COS介面所需的請求簽章

#

以上是php實作單例模式的方法及意義的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn