Search code examples
phpphpstorm

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


There are few classes:

abstract class AbstractBase
{
    protected Service1 $service1;

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

class Child extends AbstractBase
{
    private Service2 $service2;
}

If I press Alt + Insert (for child class) and select Constructor, PhpStorm will show me only properties from child class. How to generate constructor with both: child and parent class, like this?

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

Solution

  • How to generate constructor with both: child and parent class

    Currently it's not possible.

    https://youtrack.jetbrains.com/issue/WI-40676 -- watch this ticket (star/vote/comment) to get notified on any progress.


    The best I can suggest right now is this:

    • In Code | Generate... menu (Alt + Insert on Windows keymap) use Override Methods... option instead.
    • Then choose __construct(). It will create the same method as parent class has.
    • You can then move caret to the private Service2 $service2; line, place it on $service2, invoke Alt + Enter menu and then choose Add constructor parameters entry.