3

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

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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);

Comments

0

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);

13 Comments

You are advocating the use of globals.
@Alin; I didn't down vote but the problem you actually answer to the title of the question, but let's say the OP misunderstood the problem, his problem is actually not using this
Did you read the question?! This is the way to use global variables in a function. Weather it's a good or a bad thing it's a totally different issue.
Still, we should be promoting best practises here. Not layzines.
@Jan Hančič Best practices depend on the situation. If the user is a beginner and doesn't know about the global statement don't you think he should learn about that first before moving on to design patterns? This is how bad programmers are born... skipping the basics.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.