ホームページ  >  記事  >  バックエンド開発  >  PHPシングルトンモードのシンプルな使用法共有

PHPシングルトンモードのシンプルな使用法共有

小云云
小云云オリジナル
2018-02-12 10:02:021293ブラウズ

この記事では、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 デザイン モードのファクトリー モードとシングルトン モードの違い

以上がPHPシングルトンモードのシンプルな使用法共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。