Dependency Injection Reinventing how you manage PHP classes
What is DI?              DI?
The Really Short Version Dependency Injection means giving an object its      instance variables. Really. Thats it.       ...
Why This Kolaveri DI?
Why This Kolaveri DI? CHANGE
Why DI?•   Maintainable•   Extendible•   Flexible•   Configurable•   Testable•   Reusable•   Interoperable
A Real Life Example
Don’t Panic!“Dependency Injection” is a 25-dollar    term for a 5-cent concept.
A PHP Example
class MySqlDB {     private $_link;     public function __construct($host, $username, $password, $database) {         $thi...
define(MYSQL_HOST, localhost);define(MYSQL_USER, root);define(MYSQL_PASS, );define(MYSQL_DB, test);class User {    private...
Options// You can hardcode itpublic function __construct() {    $this->_db = new MySqlDB(localhost, root, , test);}// You ...
What if I want to use a differentdatabase like MongoDB or SQLite
Hey wait, I can improve it!
class User {    protected $_db;    protected $_info = array();    public function __construct() {        $registry = Regis...
Smart Huh???
So, now User depends      on Registry
Let’s do it with DI
class User {    protected $_db;    protected $_info = array();    public function __construct($database) {        $this->_...
th ere  are But        ro oms  forst ill   im pro vem ent
Interface
Ever hear          d ofType Hint          ing
Type Hinting        Since PHP 5.1public function test(OtherClass $otherClass) {}public function testInterface(Interface $i...
interface Database {    public function insert(array $data, $table);}class User {    protected $_db;    protected $_info =...
MySQLclass MySqlDB implements Database {     protected $_link;     public function __construct($host, $username, $password...
MongoDBclass MongoDB implements Database {    // ...     public function insert(array $data, $table) {         // Save the...
SQLiteclass SQLiteDB implements Database {    // ...     public function insert(array $data, $table) {         // Save the...
TestDBclass TestDB implements Database {    protected $_data = array();    public function insert(array $data, $table) {  ...
DI Contai          ner
Twittee   A DI Container in a Tweetusing the power of                           PHP 5.3class Container {    protected $s=a...
Container$c = new Container();$c->mysql = function ($c) {    return new MySqlDB(localhost, root, , test);}$c->user = funct...
More...http://components.symfony-project.org/dependency-injection/                http://pimple.sensiolabs.org/           ...
Question?
Thank You                @rifat         rifat@facebook.com          http://VistaArc.com/        http://OmicronLab.com/
Upcoming SlideShare
Loading in …5
×

Dependency Injection

2,593 views

Published on

Dependency Injection, Reinventing how you manage PHP classes

Published in: Technology
3 Comments
2 Likes
Statistics
Notes
No Downloads
Views
Total views
2,593
On SlideShare
0
From Embeds
0
Number of Embeds
220
Actions
Shares
0
Downloads
69
Comments
3
Likes
2
Embeds 0
No embeds

No notes for slide

Dependency Injection

  1. 1. Dependency Injection Reinventing how you manage PHP classes
  2. 2. What is DI? DI?
  3. 3. The Really Short Version Dependency Injection means giving an object its instance variables. Really. Thats it. - James Shore
  4. 4. Why This Kolaveri DI?
  5. 5. Why This Kolaveri DI? CHANGE
  6. 6. Why DI?• Maintainable• Extendible• Flexible• Configurable• Testable• Reusable• Interoperable
  7. 7. A Real Life Example
  8. 8. Don’t Panic!“Dependency Injection” is a 25-dollar term for a 5-cent concept.
  9. 9. A PHP Example
  10. 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. 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. 12. Options// You can hardcode itpublic function __construct() { $this->_db = new MySqlDB(localhost, root, , test);}// You can configure it with an arraypublic function __construct($config) { $this->_db = new MySqlDB($config[host], $config[user], $config[pass], $config[db]);}// And, What we saw earlierpublic function __construct() { $this->_db = new MySqlDB(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DB);}
  13. 13. What if I want to use a differentdatabase like MongoDB or SQLite
  14. 14. Hey wait, I can improve it!
  15. 15. class User { protected $_db; protected $_info = array(); public function __construct() { $registry = RegistrySingleton::getInstance(); $this->_db = $registry->database; } 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);
  16. 16. Smart Huh???
  17. 17. So, now User depends on Registry
  18. 18. Let’s do it with DI
  19. 19. class User { protected $_db; protected $_info = array(); public function __construct($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);
  20. 20. th ere are But ro oms forst ill im pro vem ent
  21. 21. Interface
  22. 22. Ever hear d ofType Hint ing
  23. 23. Type Hinting Since PHP 5.1public function test(OtherClass $otherClass) {}public function testInterface(Interface $interface) {}public function testArray(array $inputArray) {}
  24. 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. 25. MySQLclass 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. 26. MongoDBclass 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. 27. SQLiteclass 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. 28. TestDBclass 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));
  29. 29. DI Contai ner
  30. 30. Twittee A DI Container in a Tweetusing the power of PHP 5.3class Container { protected $s=array(); function __set($k, $c) { $this->s[$k]=$c; } function __get($k) { return $this->s[$k]($this); }}
  31. 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();
  32. 32. More...http://components.symfony-project.org/dependency-injection/ http://pimple.sensiolabs.org/ http://twittee.org/
  33. 33. Question?
  34. 34. Thank You @rifat rifat@facebook.com http://VistaArc.com/ http://OmicronLab.com/

×