Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Inversion of Control

I'm watching a video explaining the basics of laravel's IoC container, and I'm having trouble understanding what this code is doing. Specifically, I don't understand the parameter in the constructor function of the UserRepository class. I haven't had much luck finding an example of this syntax on the php site.

http://vimeo.com/53009943

<?php

 class UserRepository {

    protected $something;

    public function __construct(Something $something)

    {

        $this->something = $something;

    }
}

class Something {}

?>
like image 656
Marc Casavant Avatar asked Jan 11 '23 20:01

Marc Casavant


1 Answers

Quoting Laravel's documentation:

There are two ways the IoC container can resolve dependencies: via Closure callbacks or automatic resolution.

But first, what is a dependency? In the code that you have posted, the UserRepository class has one dependency, which is the Something class. It means that UserRepository is going to depend on Something somewhere in its code. Instead of using it directly, by doing something like this

$something = new Something;
$something->doSomethingElse();

it is being injected in its constructor function. This technique is known as dependency injection. So, these code snippets would do the same, without and with dependency injection.

// Without DI
class UserRepository {

    public function doSomething()
    {
        $something = new Something();
        return $something->doSomethingElse();
    }


}

Now, using DI, which would be the same as you posted:

// With DI
class UserRepository {

    public function __construct(Something $something)
    {
        $this->something = $something;
    }

    public function doSomething()
    {
        return $this->something->doSomethingElse();
    }

}

You are saying that you don't understand the parameter being passed in the constructor __constructor(Something $something). That line is telling PHP that the constructor expects one parameter, $something, which must be an instance of the Something class. This is known as type hinting. Passing a parameter which is not an instance of Something (or any subclass), will throw an exception.


Finally, let's go back to the IoC container. We've said before that its function is to resolve dependencies, and it can do it in two ways.

First one, Closure callbacks:

// This is telling Laravel that whenever we do
// App::make('user.repository'), it must return whatever
// we are returning in this function
App::bind('UserRepository', function($app)
{
    return new UserRepository(new Something);
});

Second one, automatic resolution

 class UserRepository {

    protected $something;


    public function __construct(Something $something)

    {

        $this->something = $something;

    }
}

// Now when we do this
// Laravel will be smart enough to create the constructor
// parameter for you, in this case, a new Something instance
$userRepo = App::make('UserRepository');

This is specially helpful and allows your classes to be more flexible, when using interfaces as parameters for your constructors.

like image 79
Manuel Pedrera Avatar answered Jan 14 '23 09:01

Manuel Pedrera