PHP Classes

File: php/php_di_pattern.php

Recommend this page to a friend!
  Classes of Kabir Hossain   PHP CodeIgniter Tips Tricks   php/php_di_pattern.php   Download  
File: php/php_di_pattern.php
Role: Auxiliary script
Content type: text/plain
Description: Configuration script
Class: PHP CodeIgniter Tips Tricks
Collection of tips and examples to use CodeIgniter
Author: By
Last change:
Date: 21 days ago
Size: 1,537 bytes
 

Contents

Class file image Download
//Dependency injection is a big word for "I have some more parameters in my constructor".

//It's what you did before the awfull Singleton wave when you did not like globals :

class User {
    private $_db;
    function __construct($db) {
        $this->_db = $db;
    }
}

$db = new Db();
$user = new User($db);

//Now, the trick is to use a single class to manage your dependencies, something like that :

class DependencyContainer
{
    private _instances = array();
    private _params = array();

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

    public function getDb()
    {
        if (empty($this->_instances['db'])
            || !is_a($this->_instances['db'], 'PDO')
        ) {
            $this->_instances['db'] = new PDO(
                $this->_params['dsn'],
                $this->params['dbUser'],
                $this->params['dbPwd']
            );
        }
        return $this->_instances['db'];
    }
}

class User
{
    private $_db;
    public function __construct(DependencyContainer $di)
    {
         $this->_db = $di->getDb();
    }
}

$dependencies = new DependencyContainer($someParams);
$user = new User($dependencies);
/*

You must think you just another class and more complexity. But, your user class may need something to log messages like lot of other classes. Just add a getMessageHandler function to your dependency container, and some $this->_messages = $di->getMessageHandler() to your user class. Nothing to change in the rest of your code.
*/