Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Properly using classes in other classes in php?

Should have asked someone this a long time ago.

What is the best way to use other classes within another class?

For instance, lets say I have an application class:

class Application
{
    public function displayVar() {
        echo 'hello world';
    }
}

and a database class

class Database
{
    // connects to db on construct
    public function query() {
        // queries db
    }
}

now, i want to add a function to my application class that uses a function from the db class

class Application
{
    public function displayVar() {
        echo 'hello world';
    }
    public function getVar() {
        global $db;
        $sql = foo;
        $db->query($sql);
    }
}

so then I have

$db = new Database();
$app = new Application();
$app->getVar('var');

Is there a better way of doing this? Really what I am looking for is the standard way of doing it, not another way of rigging it.

like image 837
Citizen Avatar asked Jan 07 '10 02:01

Citizen


3 Answers

There are a couple of ways of doing that. Global variables is certainly one way and the most looked down upon too. You can create a Singleton and all other classes that need database access would call upon this singleton.

final class Database {
    private static $connection;

    public static function getInstance() {
        if(self::$connection == NULL) {
            self::$connection = // init your database connection
        }
        return self::$connection;
    }
}

And use this database connection object in whatever class needs it.

class Application {
    public function displayVar() {
        echo 'hello world';
    }
    public function getVar() {
        $db = Database::getInstance();
        $sql = foo;
        $db->query($sql);
    }
}

This is all well for a start and a great step beyond using global variables, but you can do better with Dependency Injection. Dependency Injection is a simple concept that if a class has any external dependencies, such as the database connection in your example, you explicitly pass those to the needy class in its constructor or a method. So the new code would look something like Jonathan's solution. A major advantage of using dependency injection is in unit testing, where you can easily replace this actual database object with a mock object and pass it to whoever needs it.

class Application {
    private $db;

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

    public function displayVar() {
        echo 'hello world';
    }

    public function getVar() {
        $sql = foo;
        $this->db->query($sql);
    }
}

For smaller projects, you can easily do it yourself. For large projects, there are various DI frameworks available for PHP

like image 79
Anurag Avatar answered Nov 14 '22 22:11

Anurag


$db could be a property of your Application class. Any reference to it from within an instance of Application would be done via $this - $this->db

class Application {

  private $db = null;

  public function setDB($name) {
    $this->db = new Database($name);
  }

}
like image 24
Sampson Avatar answered Nov 14 '22 21:11

Sampson


Include the class file (or set up autoinclude) in each PHP file that needs the class in question. Then instantiate it as needed.

If you need to have a "common" instance of an object, you can look at the Singleton and Factory patterns:

Singleton Pattern Factory Pattern

like image 30
Peter Loron Avatar answered Nov 14 '22 23:11

Peter Loron