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
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.
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);
This is the most common method:
public function DoSomething($plogin_id){
global $dbh;
...
Alternative approach:
Create a singleton for the database connection. And you use that instead of a global variable:
$stmt = MyDBConn::getInstance()->prepare($sql);