O'Reilly logo

Learning Laravel 4 Application Development by Hardik Dangar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Dependency Injection

Dependency Injection is a software-design pattern which is used to remove hard dependencies from code, and it allows us to change that dependency without affecting the actual implementation.

Let's make it simpler with an example. We have our Car class defined as follows:

class Car
{
  function __construct()
  {
    $this->_database = DB::getInstance();

    // OR infamous Global $DB way

    Global $db;
    $this->_database = $db;
  }

}

Here, database is our hard dependency. We have bound the Car class with a database object. Assume that you want to use the Car class in one of your other projects and it uses a different database such as MongoDB or MSSQL. Apart from this, you also have to create a singleton DB object to access your data, even when ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required