Yeah, that's clear that $this->userService always returns the same instance, but this instance is a part of $this but of DI.
How does get method of injectable work? Does it create an instance right when it calls property? If yes than it should be no difference with $this->di->get(...) at the first call.
What we do is:
-
Create DI and define injections etc.
$di->set('userService', array(.....))
We inject dependencies as properties.
-
$ctrl = new MyController();
$ctrl->setDI($di) (our $di)
- $ctrl->someMethod();
function someMethod() // of MyConroller
{
$this->userService->;
and
$this->di->get('userService')->
}
In this case, when we write $this->userService, shouldnt it create new instance? Yes, if we will write the smae $this->userService it's the same instance. So what is the difference between $this->userService and $this->di->get('userService') in this case?