Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserving auto-completion abilities with Symfony2 Dependency Injection

I'm using PHP Storm as my IDE, but I believe that other IDE's such as Netbeans will have the same issue as I'll explain below.

When using a framework like Symfony2, we have the wonderful world of Dependency Injection added. So objects can simply be instantiated using code like the following snippet:

$myThingy = $this->get('some_cool_service');

This is very handy, as objects are already configured beforehand. The one problem is, that auto-completion breaks entirely in basically any PHP IDE, as the IDE does not know what type the get() method is returning.

Is there a way to preserve auto-completion? Would creating for example an extension of Controller be the answer? For example:

class MyController extends Controller {
    /**
     * @return \MyNamespace\CoolService
     */
    public getSomeCoolService() {
        return new CoolService();
    }
}

and then for application controllers, specify MyController as the base class instead of Controller?

What about using a Factory class, or any other possible methods?

like image 788
josef.van.niekerk Avatar asked Aug 27 '11 11:08

josef.van.niekerk


1 Answers

It is more involving, but you can still do this with eclipse PDT:

$myThingy = $this->get('some_cool_service');
/* @var $myThingy \MyNamespace\CoolService */

UPDATE: The example on this page shows you may also use the other way round with phpStorm:

$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */
like image 161
greg0ire Avatar answered Oct 10 '22 06:10

greg0ire