PHP Classes

No DI between car and automobile...

Recommend this page to a friend!

      PHP Dependency Injection Container  >  PHP Dependency Injection Container package blog  >  Improving Your PHP Pr...  >  All threads  >  No DI between car and automobile...  >  (Un) Subscribe thread alerts  
Subject:No DI between car and automobile...
Summary:No DI between car and automobile... but inheritance
Messages:3
Author:steven
Date:2015-11-03 21:47:45
 

  1. No DI between car and automobile...   Reply   Report abuse  
Picture of steven steven - 2015-11-03 21:47:45
The example of car and automobile is not a good one.
car is an "instance" of automobile, it inherits from it.
For me it is nonsense to set a DI between them...

  2. Re: No DI between car and automobile...   Reply   Report abuse  
Picture of Samuel Adeshina Samuel Adeshina - 2015-11-04 17:48:24 - In reply to message 1 from steven
That's the idea of Dependency Injection. The Car class "MAY" not need every property and behaviour built into the automobile class, what's the point of "INHERITING" everything when you can just inject the automobile object into your car class and call the necessary behaviours and properties you need. It's just an analogy and it's a very good one

  3. Re: No DI between car and automobile...   Reply   Report abuse  
Picture of Samuel Adeshina Samuel Adeshina - 2015-11-04 17:51:58 - In reply to message 2 from Samuel Adeshina
And then think of a situation where the Car class does not only need to inherit from the automobile class, it also needs to inherit from maybe an "engine" class or a "security" class or even all three! MULTIPLE INHERITANCES?

With DI, you can just pass an instance of all three objects into a setter method or even the constructor.