jos*_*erk 15 php symfony phpstorm
我正在使用PHP Storm作为我的IDE,但我相信其他IDE如Netbeans将会遇到与我将在下面解释的相同的问题.
当使用像Symfony2这样的框架时,我们添加了依赖注入的精彩世界.因此,可以使用类似以下代码段的代码简单地实例化对象:
$myThingy = $this->get('some_cool_service');
Run Code Online (Sandbox Code Playgroud)
这非常方便,因为事先已经配置了对象.一个问题是,自动完成基本上完全在任何PHP IDE中断,因为IDE不知道get()方法返回什么类型.
有没有办法保留自动完成?创建例如Controller的扩展会是答案吗?例如:
class MyController extends Controller {
/**
* @return \MyNamespace\CoolService
*/
public getSomeCoolService() {
return new CoolService();
}
}
Run Code Online (Sandbox Code Playgroud)
然后对于应用程序控制器,将MyController指定为基类而不是Controller?
使用Factory类或任何其他可能的方法怎么样?
gre*_*ire 15
它涉及更多,但您仍然可以使用eclipse PDT执行此操作:
$myThingy = $this->get('some_cool_service');
/* @var $myThingy \MyNamespace\CoolService */
Run Code Online (Sandbox Code Playgroud)
更新:此页面上的示例显示您也可以使用与phpStorm相反的方式:
$myThingy = $this->get('some_cool_service');
/* @var \MyNamespace\CoolService $myThingy */
Run Code Online (Sandbox Code Playgroud)
您可以在控制器中定义私有属性
class MyController extends Controller
{
/**
* @var \Namespace\To\SomeCoolService;
*/
private $my_service;
public function myAction()
{
$this->my_service = $this->get('some_cool_service');
/**
* enjoy your autocompletion :)
*/
}
}
Run Code Online (Sandbox Code Playgroud)
我使用base Controller类作为bundle.您需要在方法中注释返回.至少这适用于Eclipse.
/**
* Gets SomeCoolService
*
* @return \Namespace\To\SomeCoolService
*/
protected function getSomeCoolService()
{
return $this->get('some_cool_service');
}
Run Code Online (Sandbox Code Playgroud)
我不喜欢/*var ...*/,因为它对代码的影响太大了.我不喜欢私有属性,因为你可能错误地认为服务已经加载.
| 归档时间: |
|
| 查看次数: |
4369 次 |
| 最近记录: |