The document discusses dependency injection in PHP. It begins by defining dependency injection as giving an object its instance variables. It then discusses why dependency injection is used, including that it makes code more maintainable, extensible, flexible, configurable, testable, reusable and interoperable. The document provides a PHP example to demonstrate dependency injection and discusses additional options like using interfaces and type hinting. It explains how dependency injection allows switching between different database implementations easily and provides examples for MySQL, MongoDB and SQLite. Finally, it discusses using a DI container to further simplify managing object dependencies.
10. class MySqlDB {
private $_link;
public function __construct($host, $username, $password, $database) {
$this->_link = mysql_connect($host, $username, $password);
mysql_select_db($database);
}
public function insert($data, $table) {
array_map('mysql_real_escape_string', $data);
$query = 'INSERT INTO `' . $table .
'` (`' . implode('`,`', array_keys($data)) . '`)' .
'VALUES ("' . implode('","', $data) . '" )';
return mysql_query($query, $this->_link);
}
// ...
}
11. define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', 'root');
define('MYSQL_PASS', '');
define('MYSQL_DB', 'test');
class User {
private $_db;
private $_info = array();
public function __construct() {
$this->_db = new MySqlDB(MYSQL_HOST, MYSQL_USER, MYSQL_PASS,
MYSQL_DB);
}
public function register($name, $email, $age, $sex) {
$this->_info = compact('name', 'email', 'age', 'sex');
$this->_db->insert($this->_info, 'users');
}
// ...
}
$user = new User();
$user->register('Tasneem', 'tasmee@fb.me', 18, 'female');
12. Options
// You can hardcode it
public function __construct() {
$this->_db = new MySqlDB('localhost', 'root', '', 'test');
}
// You can configure it with an array
public function __construct($config) {
$this->_db = new MySqlDB($config['host'], $config['user'],
$config['pass'], $config['db']);
}
// And, What we saw earlier
public function __construct() {
$this->_db = new MySqlDB(MYSQL_HOST, MYSQL_USER, MYSQL_PASS,
MYSQL_DB);
}
13. What if I want to use a different
database like MongoDB or SQLite
23. Type Hinting
Since PHP 5.1
public function test(OtherClass $otherClass) {
}
public function testInterface(Interface $interface) {
}
public function testArray(array $inputArray) {
}
24. interface Database {
public function insert(array $data, $table);
}
class User {
protected $_db;
protected $_info = array();
public function __construct(Database $database) {
$this->_db = $database;
}
public function register($name, $email, $age, $sex) {
$this->_info = compact('name', 'email', 'age', 'sex');
$this->_db->insert($this->_info, 'users');
}
// ...
}
$mysql = new MySqlDB('localhost', 'root', '', 'test');
$user = new User($mysql);
$user->register('Tasneem', 'tasmee@fb.me', 18, 'female');
25. MySQL
class MySqlDB implements Database {
protected $_link;
public function __construct($host, $username, $password, $database) {
$this->_link = mysql_connect($host, $username, $password);
mysql_select_db($database);
}
public function insert(array $data, $table) {
array_map('mysql_real_escape_string', $data);
$query = 'INSERT INTO `' . $table .
'` (`' . implode('`,`', array_keys($data)) . '`)' .
'VALUES ("' . implode('","', $data) . '" )';
return mysql_query($query, $this->_link);
}
// ...
}
26. MongoDB
class MongoDB implements Database {
// ...
public function insert(array $data, $table) {
// Save the passed array using MongoDB
}
// ...
}
$mongoDb = new MongoDB('localhost', 'root', '', 'test');
$user = new User($mongoDb);
$user->register('Tasneem', 'tasmee@fb.me', 18, 'female');
27. SQLite
class SQLiteDB implements Database {
// ...
public function insert(array $data, $table) {
// Save the passed array using SQLite
}
// ...
}
$sqlite = new SQLiteDB('app.db', 'test');
$user = new User($sqlite);
$user->register('Tasneem', 'tasmee@fb.me', 18, 'female');
28. TestDB
class TestDB implements Database {
protected $_data = array();
public function insert(array $data, $table) {
$this->_data[$table] = $data;
}
public function get($table) {
return $this->_data[$table];
}
}
$fakeDb = new TestDB();
$user = new User($fakeDb);
$user->register('Tasneem', 'tasmee@fb.me', 18, 'female');
print_r($fakeDb->get('users'));
30. Twittee
A DI Container in a Tweet
using the power of PHP 5.3
class Container {
protected $s=array();
function __set($k, $c) { $this->s[$k]=$c; }
function __get($k) { return $this->s[$k]($this); }
}
31. Container
$c = new Container();
$c->mysql = function ($c) {
return new MySqlDB('localhost', 'root', '', 'test');
}
$c->user = function ($c) {
$db = $c->mysql;
return new User($db);
}
// When you need a user
$user = $c->user;
// Instead of
$user = new User();