6

I'm trying to learn the concepts of PHP OOP and I've watched a number of videos on the topic. In many of them they show an example like this :

class Person
{
    private $name;
    private $age;
    function __construct($name, $age)
    {
        $this->name = $name;
        $this->age = $age;
    }

}

class Business
{
    private $person;

    function __construct(Person $person)
    {
        $this->person = $person;
    }
}

So the problem is that one time they refer to this as Dependency Injection , other time they call it Type Hinting and third time they give this as Composition . So what exactly does this example represent ? Can you please explain the difference between them ?

user3102290
  • 145
  • 9
  • 4
    Type Hinting: Adding a type in front of the variable in a functions parameters i.e. `(Person $person)` – RiggsFolly Feb 23 '17 at 09:03
  • 2
    Dependancy Injection: Passing the Person object to the Business classes constructor – RiggsFolly Feb 23 '17 at 09:04
  • 2
    In Symfony 3.3 type hinting any method also provides dependency injection : ie makes available global service to that method. It is damn confusing. The verbiage changes all over the place. – RichieHH Jun 24 '17 at 08:05

2 Answers2

7

These are three different things:

Type hinting is instrumental of the two others and consists of typing an argument in a declaration:

function cluefull(\Type $instance) {
    // I know that $instance is of \Type, I can safely use \Type methods on $instance
}

Dependency injection relies on the constructor to define all dependencies needed for the object's lifetime and correct execution. Somewhat related talk about dependency injection

class Foo {
    private $instance;
    public function __construct(\Type $instance) {
        $this->instance = $instance;
    }
}

Composition is a design orientation that composes with the instances it needs to operate instead of inheriting from them, if at all possible. As such, it relies both on dependency injection and type hinting. More reading on composition

  • From what I can gather this is also not complete. In Symfony type hinting parameters in a controller also implicitly creates service injection as the Controller uses that type "hint" to access a service from the container. – RichieHH Jun 24 '17 at 08:15
  • 1
    @RichieHH hmmm.. this question is about oop, not symfony. The opinionated views of symfony are in no way a reference. "Knowing symfony" and "knowing oop" are not the same. – Félix Adriyel Gagnon-Grenier Jun 24 '17 at 13:31
-2

Dependancy injection is providing your application what it needs to function, which is any data. Most application are modular, they behave like separte components or entities. But all need to take in something to function.

So, that thing they need is their dependancy.

This can be passed via a class contructor, which is ideal as when the an object is initialized the contructor is the first function that gets called, so anything your app needs to work can be passed via the constructor. But sometimes you can pass the data directly to the method as an argument to your function/method Ex:

# Generic Input validator
class InputValidator{
    function isEmailValid($email){}
}

# Our main application 
class UserRegistration(){
    function Register($inputValidator){
        $isEmailValid = $inputValidator->isEmailValid('foo@bar.com'); 
    }
}

# Instanciating the class and providing the dependancy
(new UserRegistration)->Register(new InputValidator()); 

In the above example, the UserRegistration->Register() depends on the class InputValidator() to register a user, we could have provided the email validator directly in the UserRegistration class, but we choose to pass it as a dependancy instead make our application as a whole S.O.L.I.D compliant.

So, in short we are injecting the dependancy there. That is dependancy injection.

Type Hinting is, much simpler to understand.

Basically, if we extend our previous example and if you check Register(new InputValidator()); you can see that we passed it the class it needs to function, but someone mistakenly could also pass another class or even a string such as: Register('something'); which would break the application, since Method Register does not need a string. To prevent this, we can typehint it, in other words tell the Register function only to accept certain type of data : array, object, int ... or we can even explicitly inform it to take a class name by providing it before as

$InputValidator = new InputValidator(); 
Register(InputValidator $InputValidator);

as for composition, this is a better read that I can provide What is composition as it relates to object oriented design?

Community
  • 1
  • 1
samayo
  • 16,163
  • 12
  • 91
  • 106