简体   繁体   中英

PHP avoiding using global $database with PDO connection

I have this code below. What i want is to avoid the use of global $db since i've heard it's bad practice, and i'm looking for more robust/well implemented functionality.

connection.php

<?php
try {
    $db = new PDO('mysql:host=127.0.0.1;dbname=blop', 'blop', 'root');
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
    $error = $e->getMessage();
    echo $error;
}
?>

Admin.php

<?php
class Admin {

    public function fetch_data() {
        global $db;

        $sql = "SELECT * FROM `people` ORDER BY `id` DESC";
        $result = $db->query($sql);

        return $result;
    }

    public function fetch_row_count() {
        global $db;

        $sql = "SELECT * FROM `people`";
        $result = $db->query($sql);
        $num_rows = $result->rowCount();

        return $num_rows;
    }

    public function deleteAll() {
        global $db;

        $deleteAll = $db->prepare("DELETE FROM `people`");
        $deleteAll->execute();
    }

    public function deleteData($id) {
        global $db;

        $id = $_POST['id'];
        $deleteData = $db->prepare("DELETE FROM `people` WHERE `id` = '$id'");
        $deleteData->execute();
    }
}
?>

Dependency injection is when you pass into the class all the dependencies that the class needs, in this case the PDO. An example how this works for you in your case is below, using constructor injection (we pass in the database into the constructor). When instantiating the object, you need to do it like this $admin = new Admin($db); instead of $admin = new Admin();

Note also that in the method deleteData the line $id = $_POST['id']; should be deleted, as it not only uses another dependency( the post request), but it also overwrites the $id passed into the method, making the method signature meaningless.

Hope this helps.

class Admin {

    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function fetch_data() {

        $sql = "SELECT * FROM `people` ORDER BY `id` DESC";
        $result = $this->db->query($sql);

        return $result;
    }

    public function fetch_row_count() {

        $sql = "SELECT * FROM `people`";
        $result = $this->db->query($sql);
        $num_rows = $result->rowCount();

        return $num_rows;
    }

    public function deleteAll() {

        $deleteAll = $this->db->prepare("DELETE FROM `people`");
        $deleteAll->execute();
    }

    public function deleteData($id) {

        $deleteData = $this->db->prepare("DELETE FROM `people` WHERE `id` = '$id'");
        $deleteData->execute();
    }
}

Introducing dependency injection :

class Admin {

    protected $db;

    public function __construct(PDO $db) {
        $this->db = $db;
    }

    public function fetch_data() {
        ...
        $this->db->query(...);
        ...
    }

    ...

}

$pdo   = new PDO(...);
$admin = new Admin($pdo);
$admin->fetch_data();

Pretty simple, really. You simply explicitly pass all external dependencies into the class that needs it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM