I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better.
I've been reading the Laravel documentation for Service Containers and Laravel IoC. I understand the concept and why dependency injection is good practice but what I am failing to grasp is how such functionality is coded.
Take this example from Laravel:
class UserController extends Controller
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;
/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}
My question is how does UserRepository
get injected code wise on the PHP side? How does the Laravel framework operate in the background on this? I read that Laravel may use the reflection class in PHP to achieve this, but I am unsure?
I also understand there are also a few different ways of doing dependency injection such as using setter methods or resolving classes using a static function? I am adware of and found useful the Pimple Project to aid in my understanding but I am more after how the Laravel way works with the automatic class injection, could someone give me a quick example I can go off and build from? Because with the way Laravel works and its DIC, there is no call for UserRepository via a function, it just gets injected? - That is what I am interested in, how I can code that myself.
I read also that a proper DIC would allow you to write configs for each class in different ways, such as XML or in PHP code, a way to setup the class instance before being injected, is this correct?
And is there any difference between IoC(Inversion of Control) and DIC(Dependency Injection Container)?
Also, what is the difference between Laravel Service Container and IoC in this case?