PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

download PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

of 5

Transcript of PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

  • 7/25/2019 PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

    1/5

    Generating Code in PhpStorm

    The Generate commandGenerating FilesGenerating Code in the Editor

    Live Templates and Surround TemplatesEmmet (Zen Coding)

    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 ourclasses. 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 rightaround 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 ( on Mac OS X) in the Project Tool Window or the Navigation Bar to generate a file from aAlt+Insert CMD-N

    template. Some of the templates, like , will open a dialog where we can specify some additional code generation options.PHP Class

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

    Generating Code in the Editor

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

    Tweet

    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 therepetitive 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 variousoptions of generating code in PhpStorm.

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

    https://twitter.com/sharehttps://confluence.jetbrains.com/display/PhpStorm/File+Templates+in+PhpStormhttps://confluence.jetbrains.com/display/PhpStorm/File+Templates+in+PhpStormhttps://twitter.com/share
  • 7/25/2019 PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

    2/5

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

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

  • 7/25/2019 PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

    3/5

    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-Jon Mac OS X) to bring up a list of Live Templates that apply to the current language.

    We can generate various types of code, depending on the file type we're working with. In PHP classes, we can Override Methods fromsuper classes (including PHP's magic functions like and so forth), Implement methods from abstract classes,_getand _call

    generate constructor, getters and setters and PHP Docblocks. In a PHPUnit test class, we can generate new test methods. In HTMLand XML, we can generate specific tags. Try out the different available Generators!

  • 7/25/2019 PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

    4/5

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

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

    'Surround With' Templates work in a similar way: we can select a portion of code and press ( on Mac OS X) and select aCtrl+Alt+T Alt-CMD-T

    template we want to wrap our code in.

    Check out our tutorial on for more information about creating custom Live Templates to speedLive Templates (Snippets) in PhpStorm

    https://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStormhttps://confluence.jetbrains.com/display/PhpStorm/Live+Templates+%28Snippets%29+in+PhpStorm
  • 7/25/2019 PhpStorm GeneratingCodeinPhpStorm 200116 0040 2506

    5/5

    Emmet (Zen Coding)

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

    div#container>div.row>div.span8+div.span4>ul#menu>li*4>a[href="#"]

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

    up coding.

    Tweet

    http://emmet.io/https://twitter.com/sharehttps://twitter.com/sharehttp://emmet.io/