Support the ongoing development of Laravel.io →
Laravel.io Architecture
Last updated 2 years ago.
0

if you pass the class on your constructor you can test your functions if you don't, will be really hard test your code

Last updated 2 years ago.
0

For an extensive read, click here.

In short, the way DI works in L4 is you pass the class you want in the constructor, as you would in regular PHP. Then, somewhere in your application (I usually create a file in my app directory for this), bind the name you used in the constructor type hint to an actual class. I know – sounds a little vague. Here's an example:

class MyController extends BaseController
{
    protected $userRepo;        

    public function __construct(UserRepositoryInterface $userRepo)
    {
        $this->userRepo = $userRepo;
    }
}

Then all you have to do is bind UserRepositoryInterface to a class like so:

App:bind('UserRepositoryInterface', 'DbUserRepository');
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

rasparac rasparac Joined 18 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.