在类中使用全局变量


Use global variables in a class

我正在尝试创建一个分页类并使用类外部的变量。

但它给了我致命的错误"在非对象上调用成员函数 query()"。

这是索引文件:

$db = new DB_MySQL("localhost", "root", "", "test"); // connect to the database
include_once("pagi.php");
$pagination = new pagi();
$records = $pagination->get_records("SELECT * FROM `table`");

这是 pagi.php 文件:

class pagi {
    public function get_records($q) {
        $x = $db->query($q);
        return $db->fetch($x);
    }
}

是否可以从类内部的类外部使用此变量,而无需在类内创建新变量?

解决此问题的正确方法是将数据库对象注入到另一个类中(依赖注入):

$db = new DB_MySQL("localhost", "root", "", "test"); // connect to the database
include_once("pagi.php");
$pagination = new Paginator($db);
$records = $pagination->get_records("SELECT the, fields, you, want, to retrieve FROM `table`");
class Paginator
{    
    protected $db;
    // Might be better to use some generic db interface as typehint when available
    public function __construct(DB_MySQL $db)
    {
        $this->db = $db;
    }
    public function get_records($q) {
        $x = $this->db->query($q);
        return $this->db->fetch($x);
    }
}

解决它的另一种方法是将数据库类的实例注入到使用它的方法中:

$db = new DB_MySQL("localhost", "root", "", "test"); // connect to the database
include_once("pagi.php");
$pagination = new Paginator();
$records = $pagination->get_records("SELECT the, fields, you, want, to retrieve FROM `table`", $db);
class Paginator
{
    public function get_records($q, DB_MySQL $db) {
        $x = $db->query($q);
        return $db->fetch($x);
    }
}

选择哪种方法都取决于情况。如果只有一个方法需要数据库的实例,你可以将其注入到方法中,否则我会将其注入到类的构造函数中。

另请注意,我已将您的类从 pagi 重命名为 Paginator 。恕我直言,Paginator 是该类的更好名称,因为其他人(重新)查看您的代码很清楚。另请注意,我已将第一个字母大写。

我所做的另一件事是更改查询以选择您正在使用的字段,而不是使用"通配符"*。这与我更改类名的原因相同:人们(重新)查看您的代码将确切地知道将检索哪些字段,而无需检查数据库和/或结果。

更新

因为答案引发了关于为什么我会走依赖注入路线而不是声明对象global的讨论,我想澄清为什么我会在global关键字上使用依赖注入: 当你有这样的方法时:

function get_records($q) {
    global $db;
    $x = $db->query($q);
    return $db->fetch($x);
}

当您在某处使用上述方法时,不清楚类或方法的使用取决于$db。因此,它是一个隐藏的依赖关系。上述不好的另一个原因是因为您已将$db实例(因此DB_MySQL)类紧密耦合到该方法/类。如果您在某个时候需要使用 2 个数据库怎么办。现在,您必须遍历所有代码才能将global $db更改为global $db2。您永远不需要更改代码只是为了切换到另一个数据库。因此,您不应执行以下操作:

function get_records($q) {
    $db = new DB_MySQL("localhost", "root", "", "test");
    $x = $db->query($q);
    return $db->fetch($x);
}

同样,这是一个隐藏的依赖项,并将DB_MySQL类与方法/类紧密耦合。因此,也不可能正确地对Paginator类进行单元测试。您不是只测试单元(Paginator类),而是同时测试DB_MySQL类。如果您有多个紧密耦合的依赖项怎么办?现在你突然用所谓的单元测试测试了几个类。因此,当使用依赖注入时,您可以轻松地切换到另一个数据库类,甚至是用于测试目的的模拟数据库类。除了只测试一个单元的好处(您不必担心由于依赖关系而获得错误的结果)之外,它还将确保您的测试快速完成。

有些人可能认为 Singleton 模式是访问数据库对象的正确方法,但应该清楚的是,在阅读了上述所有内容后,Singleton 基本上只是另一种使事情global的方法。它可能看起来不同,但它具有完全相同的特征,因此与global相同的问题

尽管我同意依赖模型很好,但对于数据库,我个人使用静态连接,该连接可用于数据库类的所有实例,并且在需要时可以使用创建实例进行查询。下面是一个示例:

<?php
//define a database class
class DB {
    //the static connection.
    //This is available to all instances of the class as the same connection.
    private static $_conn;
    //store the result available to all methods
    private $result;
    //store the last query available to all methods
    private $lastQuery;
    //static connection function. connects to the database and stores that connection statically.       
    public static function connect($host, $user, $pass, $db){
        self::$_conn = mysqli_connect($host, $user, $pass, $db);
    }
    //standard function for doing queries. uses the static connnection property.
    public function query($query){
        $this->lastQuery = $query;
        $this->result = mysqli_query(self::$_conn, $query);
        //process result, return expected output.
    }
}
//create connection to the database, this connection will be used in all instances of DB class
DB::connect('local', 'DB_USER', 'DB_PASS');
//create instance to query
$test = new DB;
//do query
$test->query("SELECT * FROM TABLE");
//test function
function foo(){
    //create instance to use in this function
    $bar = new DB;
    //do query
    $bar->query("SELECT * FROM OTHER_TABLE");
    //return results
    return $bar->fetchArray();
}

这样,我可以在任何函数、方法中创建我想要的所有数据库实例......等等,并使用类的本地实例来执行我的所有查询。所有实例都使用相同的连接。

需要注意的一件事是,这只允许每个定义的类与数据库建立一次连接,但我只使用一个,所以这对我来说不是问题。

您可以将数据库连接($db)添加到get_records方法的调用中:

这里仅是相关的代码行:

第一个文件:

$records = $pagination->get_records("SELECT * FROM `table`", $db);

第二个文件:

public function get_records($q, $db) {

到目前为止,其他答案肯定比使用全局答案更可取,因为这会破坏您的封装(例如,您需要在调用该方法之前定义该对象)。

最好

在方法签名中强制执行,或者不使用类。