久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <tfoot id='46qKg'></tfoot>

    2. <i id='46qKg'><tr id='46qKg'><dt id='46qKg'><q id='46qKg'><span id='46qKg'><b id='46qKg'><form id='46qKg'><ins id='46qKg'></ins><ul id='46qKg'></ul><sub id='46qKg'></sub></form><legend id='46qKg'></legend><bdo id='46qKg'><pre id='46qKg'><center id='46qKg'></center></pre></bdo></b><th id='46qKg'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='46qKg'><tfoot id='46qKg'></tfoot><dl id='46qKg'><fieldset id='46qKg'></fieldset></dl></div>

        <bdo id='46qKg'></bdo><ul id='46qKg'></ul>
      <legend id='46qKg'><style id='46qKg'><dir id='46qKg'><q id='46qKg'></q></dir></style></legend>
    3. <small id='46qKg'></small><noframes id='46qKg'>

        PHP 和 PDO 類問題

        PHP and PDO class question(PHP 和 PDO 類問題)

        • <tfoot id='Pr8vR'></tfoot>
          • <i id='Pr8vR'><tr id='Pr8vR'><dt id='Pr8vR'><q id='Pr8vR'><span id='Pr8vR'><b id='Pr8vR'><form id='Pr8vR'><ins id='Pr8vR'></ins><ul id='Pr8vR'></ul><sub id='Pr8vR'></sub></form><legend id='Pr8vR'></legend><bdo id='Pr8vR'><pre id='Pr8vR'><center id='Pr8vR'></center></pre></bdo></b><th id='Pr8vR'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='Pr8vR'><tfoot id='Pr8vR'></tfoot><dl id='Pr8vR'><fieldset id='Pr8vR'></fieldset></dl></div>
              <bdo id='Pr8vR'></bdo><ul id='Pr8vR'></ul>

              <small id='Pr8vR'></small><noframes id='Pr8vR'>

                <tbody id='Pr8vR'></tbody>

                  <legend id='Pr8vR'><style id='Pr8vR'><dir id='Pr8vR'><q id='Pr8vR'></q></dir></style></legend>

                1. 本文介紹了PHP 和 PDO 類問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我對 OOP 風格的 PHP 很陌生,我也在嘗試實現 PDO.我在網上找到了這個處理數據庫連接的不錯的小類,但是我不知道如何從另一個類訪問它.代碼如下:

                  I am very new to OOP styled PHP, and I am trying to implement PDO as well. I found this nice little class online which handles the database connection, however I have no idea how to access it from another class. Here is the code:

                    class PDO_DBConnect {
                      static $db ;
                      private $dbh ;
                      private function PDO_DBConnect () {
                          $db_type = 'mysql';  //ex) mysql, postgresql, oracle
                          $db_name = 'postGal';
                          $user = 'user' ;
                          $password = 'pass' ;
                          $host = 'localhost' ;
                          try {
                              $dsn = "$db_type:host=$host;dbname=$db_name";
                              $this->dbh = new PDO ( $dsn, $user, $password);
                              $this->dbh->setAttribute(PDO::ATTR_PERSISTENT, true);
                              $this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                          } catch ( PDOException $e ) {
                              print "Error!: " . $e->getMessage () . "
                  " ;
                              die () ;
                          }
                      }
                  
                      public static function getInstance (  ) {
                          if (! isset ( PDO_DBConnect::$db )) {
                              PDO_DBConnect::$db = new PDO_DBConnect ( ) ;
                          }
                          return PDO_DBConnect::$db->dbh;
                      }
                    }
                  
                    $db_handle = PDO_DBConnect::getInstance(); 
                  
                      class Person 
                    {
                      function __construct()
                      {
                        $STMT = $db_handle->prepare("SELECT title FROM posts WHERE id = ? AND author = ? LIMIT 20");
                        $STMT->execute(array('24', 'Michael'));
                  
                        while ($result = $STMT->fetchObject()) 
                        {
                          echo $result->title;
                          echo "<br />";
                        }  
                      }
                    }
                  

                  如何訪問我的 Person 類中的 $db_handle 變量?我是否必須在 Person 類中實例化變量?如果是這樣,那是否意味著我將始終必須將其稱為 $this->db_handle ?我希望避免這種情況.(我對類的變量作用域仍然只有非常基本的了解)

                  How can I gain access to the $db_handle variable inside of my Person class? Do i have to instantiate the variable inside of the Person class? If so, does that mean I will always have to call it as $this->db_handle ? I was hoping to avoid that. (I still only have a very basic understanding of variable scope with classes)

                  推薦答案

                  有(至少)三種方法來處理這個問題.最便攜且經常被推薦的方法稱為依賴注入",您可以通過它的 __construct() 將數據庫句柄傳遞到您的類中,并將其存儲在類變量中.需要使用 $this->db 訪問它,就像您不想做的那樣.

                  There are (at least) three ways to handle this. The most portable, and oft recommended is called "dependency injection", whereby you pass the database handle into your class via its __construct() and store it in a class variable. Requires accessing it with $this->db like you didn't want to have to do.

                  class Person {
                    // Member to hold the db handle
                    public $db;
                  
                    public function __construct($db_handle) {
                      // Assign the handle to a class member variable in the constructor
                      $this->db = $db_handle;
                    }
                    public function otherFunc() {
                      $this->db; // do something
                    }
                  }
                  
                  $person = new Person($db_handle);
                  

                  下一個方法是在構造函數中實例化 $db_handle 而不是將其傳入.這有點難以測試和調試.

                  Next method would be to instantiate the $db_handle inside the constructor rather than passing it in. This is a little harder to test and debug.

                  class Person {
                     public $db;
                     public function __construct() {
                        $this->db = PDO_DBConnect::getInstance();
                     }
                  }
                  

                  最后,您可以在課堂中使用 $db_handle 作為 global 調用它.這可能是最難閱讀和調試的.

                  Finally, you can call $db_handle as a global whenever you use it in your class. This is perhaps the hardest to read and debug.

                  class Person {
                    public function __construct() {
                      global $db_handle;
                    }
                    public function otherFunc() {
                      global $db_handle;
                      $db_handle; // do something
                    }
                  }
                  

                  這篇關于PHP 和 PDO 類問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

                    <small id='dgxXW'></small><noframes id='dgxXW'>

                    <legend id='dgxXW'><style id='dgxXW'><dir id='dgxXW'><q id='dgxXW'></q></dir></style></legend>
                      • <bdo id='dgxXW'></bdo><ul id='dgxXW'></ul>

                          <tfoot id='dgxXW'></tfoot>

                          1. <i id='dgxXW'><tr id='dgxXW'><dt id='dgxXW'><q id='dgxXW'><span id='dgxXW'><b id='dgxXW'><form id='dgxXW'><ins id='dgxXW'></ins><ul id='dgxXW'></ul><sub id='dgxXW'></sub></form><legend id='dgxXW'></legend><bdo id='dgxXW'><pre id='dgxXW'><center id='dgxXW'></center></pre></bdo></b><th id='dgxXW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='dgxXW'><tfoot id='dgxXW'></tfoot><dl id='dgxXW'><fieldset id='dgxXW'></fieldset></dl></div>
                              <tbody id='dgxXW'></tbody>

                          2. 主站蜘蛛池模板: 成人免费影院 | 交专区videossex农村 | 久久一区二区视频 | 成人在线播放网站 | 成人精品视频99在线观看免费 | 不卡的av一区| 亚洲va欧美va人人爽午夜 | 日韩激情网 | 在线观看日韩精品视频 | 在线观看av网站永久 | 欧美又大粗又爽又黄大片视频 | 欧洲一区在线观看 | 久久久久国 | 在线免费毛片 | 在线亚洲免费视频 | 成人免费毛片在线观看 | 午夜精品久久久久久不卡欧美一级 | 在线四虎 | 伊人免费网 | www.久久.com | 岛国av免费看 | 欧美一级片在线看 | 激情久久网 | 91精品国产麻豆 | 亚洲国产aⅴ成人精品无吗 国产精品永久在线观看 | 中文字幕一区二区在线观看 | 亚洲精品福利视频 | 91免费在线| 亚洲精品九九 | 夜夜操操操 | 天天久久 | 免费看片在线播放 | 韩日在线观看视频 | 91精品久久久久久综合五月天 | 先锋av资源在线 | 成人免费观看视频 | 欧美精品一区二区在线观看 | 阿v视频在线观看 | 亚洲aⅴ一区二区 | 国产欧美一区二区在线观看 | 国产精品一区二区三区在线播放 |