#21 Initialize Fields

Generally arguments passed into constructors are stored in properties to be used by the rest of the class. PhpStorm can create these properties from the constructor’s method signature with only a few key strokes.

Place the cursor anywhere in the constructor’s argument list and press Alt+Enter to invoke the intention actions menu then select Initialize fields. You will be prompted to choose which fields you want to initialize; you can select one or more fields, or press Ctrl/Cmd+A to select every field in the list.

PhpStorm will create a new private property for every chosen field and name it after it’s corresponding argument, as well as initialize it with a value. It will also set up docblocks and try to identify the field’s data type on your behalf.

comments powered by Disqus