Home >Database >Mysql Tutorial >How Can I Avoid Global Variables When Accessing a Database in a PHP Class?

How Can I Avoid Global Variables When Accessing a Database in a PHP Class?

Linda Hamilton
Linda HamiltonOriginal
2025-01-18 00:17:09354browse

How Can I Avoid Global Variables When Accessing a Database in a PHP Class?

Avoid using global variables when accessing the database in PHP classes

Your code encountered an error while trying to access a global variable pagi in a $db class method. This error indicates that the query() method was called on a non-object, which indicates that the $db variable is not available in the class.

Dependency Injection Solution

The recommended way to solve this problem is to use dependency injection and pass the database object as a parameter into the class instead of relying on global variables. An example is as follows:

<code class="language-php">$db = new DB_MySQL("localhost", "root", "", "test");
include_once("pagi.php");

$pagination = new Paginator($db);
$records = $pagination->get_records("SELECT * FROM `table`");</code>
<code class="language-php">class Paginator
{
    private $db;

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

    public function get_records($q)
    {
        $x = $this->db->query($q);
        return $this->db->fetch($x);
    }
}</code>

In this example, we create a new class called Paginator that passes in the DB_MySQL object as a dependency through the constructor. This injects the database instance into the class, making it available to methods within it.

Advantages of Dependency Injection

Using dependency injection has the following advantages:

  • Decoupling: It decouples a class from its direct dependency on the DB_MySQL class, making it more flexible and easier to test.
  • Testability: Unit testing becomes easier because you can mock dependencies and isolate test classes.
  • Avoid hidden dependencies: Unlike using the global keyword, dependency injection makes it clear that the class depends on a specific dependency.

Other methods

Another way to access the database object is to inject it directly into the method that requires it, like this:

<code class="language-php">$db = new DB_MySQL("localhost", "root", "", "test");
include_once("pagi.php");

$pagination = new Paginator();
$records = $pagination->get_records("SELECT * FROM `table`", $db);</code>
<code class="language-php">class Paginator
{
    public function get_records($q, DB_MySQL $db)
    {
        $x = $db->query($q);
        return $db->fetch($x);
    }
}</code>

However, dependency injection via constructors is generally preferred as it provides better encapsulation and testability.

The above is the detailed content of How Can I Avoid Global Variables When Accessing a Database in a PHP Class?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn