dongliugu8843 2014-07-24 10:19
浏览 64
已采纳

PHP避免使用带有PDO连接的全局$数据库

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();
    }
}
?>
  • 写回答

2条回答 默认 最新

  • dongpa2000 2014-07-24 10:32
    关注

    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();
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大