How to Generate constructors in Magento 2
Because of Dependency Injection pattern Magento 2 have huge constructors for every class. World best PHP IDE PHP Storm can help you generate constructors and save lots of time in Magento 2 development.
With constructor generator you will be able to create constructors with arguments, with the value assigned to the field variables.
To generate a constructor
-
Go the main menu and select Code | Generate, or you can right-click the editor and choose Generate from the context menu. Besides, it is possible to use Alt+Insert keyboard shortcut.
-
Then, select Constructor option in the pop-up list displayed in the editor.
-
In case when the class in question contains fields, you should select the fields to be initialized by constructor.
-
Now, click OK.
Check the example below:
1 2 3 4 5 6 7 8 9 |
public class MyClass { int aInteger; double bDouble; public MyClass(int myAIntegerParam, double myBDoubleParam) { aInteger = myAIntegerParam; bDouble = myBDoubleParam; } } |
More tips from Magento 2 Developer’s Cookbook
For further information, follow this link: