PhpStorm:如何生成具有所有 parent 属性和 child 的构造函数

PhpStorm: how to generate constructor with all parent properties and child too

很少class是:

abstract class AbstractBase
{
    protected Service1 $service1;

    public function __construct(Serivice1 $s)
    {
        $this->service1 = $s;
    }
}

class Child extends AbstractBase
{
    private Service2 $service2;
}

如果我按 Alt + Insert(对于 child class)和 select Constructor,PhpStorm 将只显示来自 child 的属性class。如何同时使用 child 和 parent class 生成构造函数,像这样?

public function __construct(Service1 $service1, Service2 $service2)
{
    parent::__construct($service1);
    $this->service2 = $service2;
}

How to generate constructor with both: child and parent class

暂时不能。

https://youtrack.jetbrains.com/issue/WI-40676 -- 观看这张票 (star/vote/comment) 以获得任何进展的通知。


我现在能建议的最好的是:

  • Code | Generate... 菜单中(Alt + Insert on Windows keymap)改用 Override Methods... 选项。
  • 然后选择__construct()。它将创建与父 class 相同的方法。
  • 然后您可以将插入符号移动到 private Service2 $service2; 行,将其放在 $service2 上,调用 Alt + Enter 菜单,然后选择 Add constructor parameters条目。