Use of PDO in classes

后端 未结 4 1034
面向向阳花
面向向阳花 2020-11-28 01:40

I have a few classes that perform some MySQL queries and prepared statements. However, I am lost in how to incorporate my PDO object within those classes. For example, I w

4条回答
  •  盖世英雄少女心
    2020-11-28 02:26

    $dbh isn't within the scope of Foo, do this instead:

    class Foo /*extends PDO*/
    {
        public $dbh;
    
        public function __construct()
        {
            $dbh = new PDO(/*...*/);
        }
    
        public function bar()
        {
            $this->dbh->prepare('SELECT * FROM table');
            return $this->dbh->execute();
        }
    }
    

    Also, Foo doesn't need to extend PDO.

提交回复
热议问题