• <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 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'>

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

      1. PHP 和 PDO 类问题

        时间:2023-10-04
          <tfoot id='46qKg'></tfoot>

        1. <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 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>
            • <small id='46qKg'></small><noframes id='46qKg'>

                    <tbody id='46qKg'></tbody>
                  本文介绍了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模板网!

                  上一篇:PDO 语句的转义参数? 下一篇:PHP、ORM、MSSQL 和 Unicode,是否可以让它们一起工作

                  相关文章

                  最新文章

                    <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 id='dgxXW'><tfoot id='dgxXW'></tfoot><dl id='dgxXW'><fieldset id='dgxXW'></fieldset></dl></div>