Dependency Injection

Hello, I have a simple question…
Why dependency injection is used with OOP?
Can anyone clear this out…

Because you have objects that depend on the presence of other objects but you don’t want to make these objects global variables. Dependency injection is a great alternative to global variables and singleton statics, both are anti-patterns to avoid as an aspiring OO programmer.

One common usage for dependency injection is when you have a domain model or data mapper that does CRUD operations, which will likely to require a database object(like a PDO instance). Without dependency injection, you either have to create such a database object in the constructor of the domain object, or use one database object as global variable. The former can lead to performance issues when you instantiate a collection of domain objects, the latter is a bad practice to avoid. By using dependency injection, you pass an instance of database object to domain object’s constructor, it does not create extra copies of database object, nor is it using any globals. In this way, you write more elegant, maintenable and unit-test friendly code.

Well, you may want to start with reading these threads:

Helps Simplify Code - http://www.sitepoint.com/forums/showthread.php?1143206-OOP-Calling-one-class-from-within-another&highlight=dependency+injection
Did I use DI correctly? - http://www.sitepoint.com/forums/showthread.php?1093869-Dependency-Injection-Container&highlight=dependency+injection

To expand a little on what Hall of Famer said, have a look at the examples below. It’s common to see code like the first example where dependencies are hard-coded into the class:


class User
{
    public function __construct()
    {
        $this->db = new PDO(...);
    }

    // ...
}

$user = new User;

Here’s how you might rewrite the class to utilise DI:


class User
{
    protected $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    // ...
}

$user = new User(new PDO(...));

By allowing the PDO dependency to be injected via the constructor, the User class is now more flexible: we could instantiate a user object which writes it’s data to a different DB just by swapping out the PDO connection that we pass in. It’s also easier to test, as we can pass in a mock DB connection.

Really, a better example would be this:


class User
{
    protected $db;
    
    public function __construct(PDO $db)
    {
        $this->db = $db;
    }
    
    // ...
}
$PDO = new PDO(...);
$PDO->dosomething();
$user = new User($PDO);
$PDO->dosomething();

Both $user class, and $PDO->dosomething() is altering and using the same variable, in this case, using the same connection. If I were to have a class where dosomething() were simply incrementing a vlaue inside the class, and dosomething() were called both in $user as well as outside $user, the value would be equivalent to how many times it was called.

thank you of of you… Iv been able to understand it… :slight_smile: ty…

Take a look at type hinting too:

http://php.net/manual/en/language.oop5.typehinting.php