Laravel中的依赖注入

Coming for a JavaScript and Java background, I find dependency injection in Laravel (or PHP) extremely confusing.

For example (Laravel 5.1), in Middleware/Authenticate the constructor takes a Guard object. I have search the codebase and the class Authenticate is never instantiated.

So where is the Guard coming from then?

Thanks, SK.

Look at the laravel documentation:

Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods. http://laravel.com/docs/5.1/container

You can instantiate Guard in your code or fetch instance of Guard from somewere and pass it to Middleware/Authenticate constructor.

EDITED : Guard is injected into the Middleware/Authenticate automatically somewere in laravel framework code at some point. You better take a look on different example of Dependency injection in Laravel where all is visibly in your code.

Defn : Dependency injection is a technique whereby one object supplies the dependencies of another object.

You could visit the link below for a detailed explanation with ioc container explanation Visit : https://www.thelearninguy.com/dependency-injection-in-laravel-using-ioc-container