Autocomplete constructor methods with class properties

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

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;


 How can I teach PHPStorm to do this automatically?

Comment actions Permalink

Hi there,

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

Comment actions Permalink

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

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).

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.

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..)


Please sign in to leave a comment.