Using PDO with other classes

雨燕双飞 提交于 2020-02-02 14:37:51

问题


I have been forcing myself to get into more OOP. I have hated it all up in till now. When i am using some simple prepare statment in PDO within another class as a method it never works. I resolved it by doing the obvious: globalising the PDO object into the method. It works, and does what i want - but if i had many many methods from loads of different classes, adding "global $db;" as the first line to alllll the functions/methods it seems quite tedious. Is there a way of integrating PDO into all classes? or at least each class- instead of every single bloody method?

Heres a very very simple example of what what curretnly works, but as i said tedious:

<?php
 $db = new PDO("mysql:host=localhost;dbname=blaa;", "blaa", "blaa");
class test{

function show($col, $id){
    global $db;
    $result = $db->prepare("SELECT ".$col." FROM products WHERE id = :id");
    $result->execute(array("id"=>$id));
    $row = $result->fetch();
    echo $row[$col];
}
}

$show = new test();
$show->show("price", 1);
?>

..so i can use my PDO in the method "show()" but if i were to add another method, i would have to put "global $db;" in it again...

So how do i not globalise it in just a method, but instead, ALL CLASSES? I tried inheriting the PDO class into the "test" class but that did not work; I tried using a constructor like:

<?php
$db = new PDO("mysql:host=localhost;dbname=blaa;", "blaa", "blaa");
class test{
    public $db;
function __construct($db){
           $this->db = $db;
    }
function show($col, $id){
    $result = $db->prepare("SELECT ".$col." FROM products WHERE id = :id");
    $result->execute(array("id"=>$id));
    $row = $result->fetch();
    echo $row[$col];
}
}

$show = new test($db);
$show->show("price", 1);
?>

but that did not work..

Any help would be appreciated!

Thanks -Wylie


回答1:


$this->db = $db;

means you assigned $db to $this->db, not the contrary!

So, you have to use $this->db, not $db in your class

$result = $this->db->prepare("SELECT ".$col." FROM products WHERE id = :id");



回答2:


"Your Common Sense" is right. But I want to add that you could and should use the singleton pattern: create a class whose purpose is to maintain one unique connection to the database.

class Database {
    private static $instance = null;

    private $pdo;
    private function __construct() {
        $this->pdo = new PDO("mysql:host=localhost;dbname=blaa;", "blaa", "blaa");
    }

    public static function get() {
        if(is_null(self::$instance))
            self::$instance = new Database();
        return self::$instance;
    }
}

Then, every time you need to access the database, instead of storing the PDO object as an instance attribute, you use:

$db = Database::get();

Your example would become:

class test {
    function __construct() {
        // You don't need this anymore, unless you have other things to do in the constructor
    }

    function show($col, $id) {
        $db = Database::get();
        $result = $db->prepare("SELECT ".$col." FROM products WHERE id = :id");
        $result->execute(array("id"=>$id));
        $row = $result->fetch();
        echo $row[$col];
    }
}

If you don't want to call Database::get in every method where you need it, you could do it once in the constructor instead.

class test {
    private $db;

    function __construct() {
        $this->db = Database::get();
    }

    function show($col, $id) {
        $result = $this->db->prepare("SELECT ".$col." FROM products WHERE id = :id");
        $result->execute(array("id"=>$id));
        $row = $result->fetch();
        echo $row[$col];
    }
}


来源:https://stackoverflow.com/questions/18163457/using-pdo-with-other-classes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!