Created
November 22, 2017 07:09
-
-
Save lastday154/c769252a4c309c7803487ba86d114799 to your computer and use it in GitHub Desktop.
Dependency Injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Database | |
| { | |
| protected $adapter; | |
| public function __construct() | |
| { | |
| $this->adapter = new MysqlAdapter; | |
| } | |
| } | |
| class MysqlAdapter | |
| { | |
| } | |
| // using DI | |
| class Database | |
| { | |
| protected $adapter; | |
| public function __construct(MysqlAdapter $adapter) | |
| { | |
| $this->adapter = $adapter; | |
| } | |
| } | |
| // Using D in SOLID "Depends on Abstraction, not depend on concretions" | |
| class Database | |
| { | |
| protected $adapter; | |
| public function __construct(AdapterInterface $adapter) | |
| { | |
| $this->adapter = $adapter; | |
| } | |
| } | |
| interface AdapterInterface {} | |
| class MysqlAdapter implements AdapterInterface | |
| { | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment