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?

0
5 comments

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
Avatar
Permanently deleted user

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

1

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
Avatar
Permanently deleted user

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

0

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.