Skip to end of metadata
Go to start of metadata

 

There is a saying, "good developers are lazy developers." It sounds harsh but there is a truth to it: we can be "lazy" by letting the IDE do the repetitive work for us so we can work on what brings value: the business logic of our applications. In this tutorial, we'll look at the various options of generating code in PhpStorm.

The Generate command

Often, a lot of development time is consumed doing nearly useless work such as generating getters and setters for a number of fields in our classes. While these getters and setters may be needed for our code, it's a time-consuming job to write them all. Copy-paste-adapt bugs are right around the corner for this type of code, too. Luckily, you can have PhpStorm generate code for you by using the Generate command.

Generating Files

We can use the context menu or Alt+Insert (CMD-N on Mac OS X) in the Project Tool Window or the Navigation Bar to generate a file from a template. Some of the templates, like PHP Class, will open a dialog where we can specify some additional code generation options.

When we click OK, the IDE will generate a new file for us and, depending on the selected template, generate boilerplate code.

Icon

Note the available templates can be modified through IDE Settings | File Templates. Check the File Templates in PhpStorm tutorial to learn how to customize and create them.

Generating Code in the Editor

We can also generate code within the editor. Pressing Alt+Insert (CMD+N on Mac OS X) in the editor will give us a list of all available code generation options.

Let's generate getters and setters. We can select the fields we want to generate them for.

After we click OK, PhpStorm will generate the code for us, including PHPDoc blocks and so on.

Icon

We can generate various types of code, depending on the file type we're working with. In PHP classes, we can Override Methods from super classes (including PHP's magic functions like _get and _call and so forth), Implement methods from abstract classes, generate constructor, getters and setters and PHP Docblocks. In a PHPUnit test class, we can generate new test methods. In HTML and XML, we can generate specific tags. Try out the different available Generators!

Live Templates and Surround Templates

While working on code, whether PHP, JavaScript or HTML, we can make use of Live Templates to insert snippets of code. Press Ctrl+J (CMD-J on Mac OS X) to bring up a list of Live Templates that apply to the current language.

When we press TAB, we can loop through the various placeholders and provide a meaningful value for them.

Using this technique, we did not have to write out the HTML tag. Instead, we could just use a Live Template (input:t in this case) and tab through some placeholders.

'Surround With' Templates work in a similar way: we can select a portion of code and press Ctrl+Alt+T (Alt-CMD-T on Mac OS X) and select a template we want to wrap our code in.

Icon

Check out our tutorial on Live Templates (Snippets) in PhpStorm for more information about creating custom Live Templates to speed up coding.

Emmet (Zen Coding)

When working in HTML or CSS, we can use Emmet (formerly known as Zen Coding) to generate tags for us. Try entering the following in an HTML document:

When we press TAB, PhpStorm expands the Emmet abbreviations into full HTML (or CSS):

  • No labels