I'm using Pimple dependency injector, and every time I use a dependency from the container, I can't help but to double check the spelling of the key used to get the dependency:
$ioc = new Pimple();
// 1. Define some object
$ioc["some-key"] = $ioc->share(function($c){ /* ... */});
// 2. Use it
$ioc["som... // Open config file and check spelling...
Does PHPStorm have some way of looking up those properties and providing auto-completion? I have considered defining all those keys using something like
define('SOME_KEY', 'some-key');
// ...
$ioc[SOME_KEY] = $ioc->share(/* ... */);
but I wonder if there's a better way.
Edit
Here's some sample code:
// project_root/library/App/Injector/Ioc.php
require_once "Pimple.php";
/** @var array|Pimple $ioc */
$ioc = new Pimple();
$ioc["version"] = "1.0.1650.63";
$ioc["location-service"] = $ioc->share(function ($c) {
return new Application_Service_Location();
}
);
It turns out that string auto-completion works fine whether or not I include /** @var array|Pimple $ioc */ before the $ioc declaration in the same file as $ioc is declared. However, since I'm using Zend Framework, I'm usually using $ioc thusly:
// project_root/Application/Bootstrap.php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
protected function _initInjector() {
$ioc = null;
require_once LIBRARY_PATH . "/MFM/Injector/ioc.php";
Zend_Registry::set("ioc", $ioc);
}
}
// project_root/Application/Controllers/SomeController.php
class Application_Controller_SomeController extends Zend_Controller_Action {
public function IndexAction() {
/** @var Pimple $ioc */
$ioc = Zend_Registry::get("ioc");
// No IDE assistance for the string "location-service"
$service = $ioc["location-service"];
}
}