6

I have a database class, which is used to make select, update, delete MySQL queries.

Now, I want to create a MySQL query inside another class, but if I define $db = new DB(); in index.php, I can't use the $db var in another class. Do I have to define the variable $db over and over again, if I want to make a query? Or is there a way to make the $db var with an object global var?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike
  • 6,854
  • 17
  • 53
  • 68

7 Answers7

12

The cleanest approach would be to aggregate the database class where needed by injecting it. All other approaches, like using the global keyword or using static methods, let alone a Singleton, is introducing tight coupling between your classes and the global scope which makes the application harder to test and maintain. Just do

// index.php
$db  = new DBClass;               // create your DB instance
$foo = new SomeClassUsingDb($db); // inject to using class

and

class SomeClassUsingDb
{
    protected $db;
    public function __construct($db)
    {
        $this->db = $db;
    }
}

Use Constructor Injection if the dependency is required to create a valid state for the instance. If the dependency is optional or needs to be interchangeable at runtime, use Setter Injection, e.g.

class SomeClassUsingDb
{
    protected $db;
    public function setDb($db)
    {
        $this->db = $db;
    }
}
Gordon
  • 312,688
  • 75
  • 539
  • 559
5

You probably want a singleton. This gives you a way to get an instance of DB anywhere in the code. Then anywhere you want to do a query, first do $db = DB::getInstance();.

An alternative is dependency injection, which passes a DB instance to all classes which need one.

Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • An example for the singleton pattern in php can be found here: http://www.weberdev.com/get_example-4002.html – Zilverdistel Sep 29 '10 at 09:03
  • To make an object globally available you only need a static method to instantiate it. It does not have to be Singleton. Both is smelly, so Dependency Injection is the way to go. – Gordon Sep 29 '10 at 09:09
0

In your index.php file use

require_once('path_to_file_with_class.php');

You may also use include_once, which will give you a warning instead of an error if the 'path_to_file_with_class.php' file is not available.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Thariama
  • 50,002
  • 13
  • 138
  • 166
0

Define it on a class (separate PHP file). Then require it for every PHP file the var is needed in.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ruel
  • 15,438
  • 7
  • 38
  • 49
0

First make your database class a singleton. And then in your new class you can do something like:

class myNewClass{
  private $_db;

  public function  __construct(){
     $this->_db = DB::getInstance();
  }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Hannes
  • 8,147
  • 4
  • 33
  • 51
0

Use the magic function __autoload().

Ben Fransen
  • 10,884
  • 18
  • 76
  • 129
-1

You might define it as global in your index.php file, and in the class constructor also put $this->db &= $GLOBALS['db'];

Zilverdistel
  • 1,571
  • 11
  • 10