Say we use a DI container directly, i.e. via functions like get() or make(), how do we achieve proper IDE support? For example I create a class instance the usual way
$server = $this->container->get(Server::class);
and I want to know what methods my Server class provides
I could use docblocks
/** @var Server $server */ $server = $this->container->get(Server::class);
This works fine in many IDE like PhpStorm:
however the docblock is somehow annoying to me. The IDE *should* really know what class instance the variable $server is, since everything it needs to know is in the class name constant I provided as parameter for the get() function.
There might be a way using the hardly known .phpstorm.meta.php file for PhpStorm, but that one is currently some sort of under construction – at least I couldn’t get it working properly. Hopefully the PhpStorm guys will extend the documentation soon!
Once again, the PHP community provides the solution: the small but pretty cool PhpStorm PHP-DI plugin does exactly what we need! Now PhpStorm knows that $server is an instance of the Server class without me needing to do any further work:Class name constants should be used wherever possible nowadays, however it also works with the Fully-qualified class name as string:
$server = $this ->container ->get('CarstenWindler\ApiGuy\Server');
But really, you shouldn’t do that. Please. Better just forget what I said. Forget it. Begone!