Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using global DB variable inside classes in PHP

How can I use global DB variable inside class? Let's say I have this in my config.php

$dbh = new PDO("mysql:host=localhost;dbname=mydb", "root", "");

and I want to use this $dbh inside class as follows (MyClass.php)

class MyClass
{
   public function DoSomething($plogin_id)
   {
        $sql = "SELECT * FROM mytable WHERE login_id = :login_id";      
        $stmt = $dbh->prepare($sql);    //line 14
        $stmt->bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
   }
}

And inside my index.php file I am using this MyClass as follows:

include "config.php";
$MyObject = new MyClass();
$login_result = $MyObject->DoSomething("admin");

It is giving me error:

Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\MyProject\admin\includes\classes\MyClass.php on line 14

like image 544
Ali Avatar asked May 03 '26 08:05

Ali


2 Answers

You have to pass the $dbh object to MyClass somehow. Since you don't want to use globals, I suggests you pass it to MyClass's constructor.

Your MyClass and index.php could look something like this:

class MyClass
{
   protected $dbh = null;

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

   public function DoSomething($plogin_id)
   {
        $sql = "SELECT * FROM mytable WHERE login_id = :login_id";      
        $stmt = $this->dbh->prepare($sql);    //line 14
        $stmt->bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
   }
}

// in your index.php
$MyObject = new MyClass ( $dbh );

This is basically a pattern called dependency injection. See this excellent tutorial for more info on what this is.

like image 120
Jan Hančič Avatar answered May 05 '26 21:05

Jan Hančič


You can introduce it into your function by using:

global $dbh;

However, it might be a better idea to add it to the class, like this:

class MyClass
{
    private $dbh;

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

    public function DoSomething($plogin_id)
    {
        $sql = "SELECT * FROM mytable WHERE login_id = :login_id";      
        $stmt = $this->dbh->prepare($sql);    //line 14
        $stmt->bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
    }
}

and then:

include "config.php";
$MyObject = new MyClass($dbh); // I'm assuming $dbh is created in config.php
$login_result = $MyObject->DoSomething("admin");

Or, introduce it into your function at call time:

class MyClass
{
    public function DoSomething($plogin_id, $dbh)
    {
        $sql = "SELECT * FROM mytable WHERE login_id = :login_id";      
        $stmt = $dbh->prepare($sql);    //line 14
        $stmt->bindParam(':login_id', $plogin_id, PDO::PARAM_STR);
    }
}

And then:

include "config.php";
$MyObject = new MyClass($dbh); // I'm assuming $dbh is created in config.php
$login_result = $MyObject->DoSomething("admin", $dbh);
like image 39
Spiny Norman Avatar answered May 05 '26 22:05

Spiny Norman