Autocomplete constructor methods with class properties

When I write a new class, I start with the class name and properties, ie.:

<?php
declare( strict_types = 1 );

namespace pci\Domain\Notification;

class Notification {

private $CallerTitle;
private $CallerLastName;
private $CallerNumber;
private $Message;
private $AgentFirstName;
private $AgentLastName;

Then I write the constructor:

public function __construct( string $CallerTitle, string $CallerLastName, string $CallerNumber, string $Message, string $AgentFirstName, string $AgentLastName ) {

$this->CallerTitle = $CallerTitle;
etc.

}

 How can I teach PHPStorm to do this automatically?

5 comments
Comment actions Permalink

Hi there,

Do other way around -- write constructor first and then let IDE generate fields and assignments.

 

https://stackoverflow.com/a/27487443/783119

https://stackoverflow.com/a/43435723/783119

1
Comment actions Permalink

Okay, so the answer would be ALT + Insert, then choose "constructor". Thanks!

1
Comment actions Permalink

Yeah -- that will also do.

I just prefer other way around as it will make typehints (PHPDoc comments) for fields as well + I have been using it for so long ... when generate constructor was not available (in current form at very least).

0
Comment actions Permalink

I don't use PHPDoc and pass the type hints to the constructor, so I still have to type these, which is okay.

0
Comment actions Permalink

You can write the start of the class, without a constructor but with all the properties, and then select Code -> Generate... -> Contructor...

In the dialog, select the properties you want to be initialized by the generated constructor (keep SHIFT or CTRL pressed to select more than one), and click OK when ready.

After this, you can customize it with all the other stuff (method calls, etc..)

0

Please sign in to leave a comment.