Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Database
- {
- // Let's pretend we have boatloads of code here
- }
- // Now, the model class NEEDS database class in order to work, we can say
- // that the model DEPENDS on the database class, since the class needs
- // database instance to work, we INJECT the database object to the model
- // class via constructor, this is called constructor injection IIRC
- class BlogModel
- {
- protected $db;
- // Inject a database instance via constructor
- public function __constructor(Database $db)
- {
- $this->db = $db;
- }
- }
- // Create an instance of database
- $db = new Database('localhost', 'root', 'dkjfkf', 'db_name');
- // Whenever we need the BlogModel, inject Database instance via the constructor
- $blogmodel = new BlogModel($db);
- // That's dependency injection in a nutshell
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement