Right Way to Access Dependency Injection From Controller


I'm studying Phalcon and got stuck on the best way to access the Dependency Injection container from inside a controller.

From inside the controller, I see a variable in my debugger..


but I also had success relying on MAGIC methods like this....


Am I supposed to use the MAGIC methods approach?

http://docs.phalconphp.com/en/latest/reference/controllers.html#injecting-services -> straight from the controller docs. ;)

Personally I find the multiple access possibilities to be a negative aspect of a framework. With at least 4 ways to get a service from the DI from a controller, you're guaranteed to have a mess of an inconsistent codebase in a team environment. Frankly the ability to pull a DI service through magic __get() is horrible...

// controller starts out like this, magically pulling 'user' DI service.
class UserController extends \Phalcon\Mvc\Controller {
    public function loginAction() {
        $this->user->doLogin(/* arguments... */);

Later on, someone who doesn't know "user" is being used via magic __get() adds a "protected $user;" to UserController. Now the loginAction() is broken because $this->user will now refer to the controller's instance variable rather than the DI service. It's very messy to wind up having a crapload of "reserved names" in a controller based on what has been added to a DI.

gte451f, also take a look at https://github.com/phalcon/phalcon-devtools/tree/master/ide . Dive into the directory matching your Phalcon version, then copy the contained Phalcon/ directory into your project's library directory in your IDE. Provide the autocomplete you're looking for. :)