Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP单例模式使用方法详解

PHP单例模式使用方法详解

墨辰丷
墨辰丷asal
2018-05-21 11:05:581208semak imbas

这篇文章主要介绍了PHP单例模式简单用法,结合数据库操作类实例形式分析了php单例模式的具体实现方法与使用技巧,需要的朋友可以参考下

具体如下:

<?php
class db {
  public $conn;
  public static $sql;
  public static $instance=null;
  private function __construct(){
    require_once(&#39;db.config.php&#39;);
    $this->conn = mysql_connect($db[&#39;host&#39;],$db[&#39;user&#39;],$db[&#39;password&#39;]);
    if(!mysql_select_db($db[&#39;database&#39;],$this->conn)){
      echo "失败";
    };
    mysql_query(&#39;set names utf8&#39;,$this->conn);
  }
  public static function getInstance(){
  if(is_null(self::$instance)){
    self::$instance = new self();
  }
    return self::$instance;
  }
  /**
  * 查询数据库
  */
  public function select($table,$condition=array(),$field = array()){
    $where=&#39;&#39;;
    if(!empty($condition)){
      foreach($condition as $k=>$v){
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $fieldstr = &#39;&#39;;
    if(!empty($field)){
      foreach($field as $k=>$v){
        $fieldstr.= $v.&#39;,&#39;;
      }
      $fieldstr = rtrim($fieldstr,&#39;,&#39;);
    } else {
      $fieldstr = &#39;*&#39;;
    }
    self::$sql = "select {$fieldstr} from {$table} {$where}";
    $result=mysql_query(self::$sql,$this->conn);
    $resuleRow = array();
    $i = 0;
    while($row=mysql_fetch_assoc($result)){
      foreach($row as $k=>$v){
        $resuleRow[$i][$k] = $v;
      }
    $i++;
    }
    return $resuleRow;
  }
  //添加一条记录
  public function insert($table,$data) {
    $values = &#39;&#39;;
    $data = &#39;&#39;;
    foreach ($data as $k=>$v) {
      $values .= $k.&#39;,&#39;;
      $datas .= "&#39;$v&#39;".&#39;,&#39;;
    }
    $values = rtrim($values,&#39;,&#39;);
    $datas = rtrim($datas,&#39;,&#39;);
    self::$sql = "INSERT INTO {$table} ({$values}) VALUES ({$datas})";
    if(mysql_query(self::$sql)) {
      return mysql_insert_id();
    } else {
      return false;
    }
  }
  //修改一条记录
  public function update($table,$data,$condition=array()){
    $where=&#39;&#39;;
    if(!empty($condition)) {
    foreach($condition as $k=>$v) {
      $where.=$k."=&#39;".$v."&#39; and ";
    }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    $updatastr = &#39;&#39;;
    if(!empty($data)) {
    foreach($data as $k=>$v) {
      $updatastr.= $k."=&#39;".$v."&#39;,";
    }
      $updatastr = &#39;set &#39;.rtrim($updatastr,&#39;,&#39;);
    }
    self::$sql = "update {$table} {$updatastr} {$where}";
    return mysql_query(self::$sql);
  }
  //删除记录
  public function delete($table,$condition) {
    $where=&#39;&#39;;
    if(!empty($condition)) {
      foreach($condition as $k=>$v) {
        $where.=$k."=&#39;".$v."&#39; and ";
      }
      $where=&#39;where &#39;.$where .&#39;1=1&#39;;
    }
    self::$sql = "delete from {$table} {$where}";
    return mysql_query(self::$sql);
  }
  public static function getLastSql() {
    echo self::$sql;
  }
}
$db = db::getInstance();
//$list = $db->select(&#39;demo&#39;,array(&#39;name&#39;=>&#39;tom&#39;,&#39;password&#39;=>&#39;ds&#39;),array(&#39;name&#39;,&#39;password&#39;));
//echo $db->insert(&#39;demo&#39;,array(&#39;name&#39;=>&#39;最近你啦&#39;,&#39;password&#39;=>&#39;123&#39;));
//echo $db->update(&#39;demo&#39;,array("name"=>&#39;xxx&#39;,"password"=>&#39;123&#39;),array(&#39;id&#39;=>1));
echo $db->delete(&#39;demo&#39;,array(&#39;id&#39;=>&#39;2&#39;));
db::getLastSql();
echo "<pre class="brush:php;toolbar:false">";
?>

相关推荐:

php实现mongoDB单例模式操作类步骤详解

PHP中单例模式与工厂模式详解及实例

PHP单例模式使用案例详解

Atas ialah kandungan terperinci PHP单例模式使用方法详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn