A look into the high-level programming operations for the PHP language

3 min read

(For more resources related to this topic, see here.)

Accessing documentation

PhpStorm offers four different operations that will help you to access the documentation: Quick Definition, Quick Documentation, Parameter Info, and External Documentation. The first one, Quick Definition, presents the definition of a given symbol. You can use it for a variable, function, method, or class. Quick Documentation allows easy access to DocBlocks. It can be used for all kinds of symbols: variables, functions, methods, and classes. The next operation, Parameter Info, presents simplified information about a function or method interface. Finally, External Documentation will help you to access the official PHP documentation available at php.com.

Their shortcuts are as follows:

  • Quick Definition (Ctrl + Shift + I, Mac: alt + Space bar)

  • Quick Documentation (Ctrl + Q, Mac: F1)

  • Parameter Info (Ctrl + P, Mac: command + P)

  • External Documentation (Shift + F1, Mac: shift + F1)

The Esc (Mac: shift + esc) hotkey will close any of the previous windows.

If you place the cursor inside the parenthesis of $s = str_replace(); and run Parameter Info (Ctrl + P, Mac: command + P), you will get the hint showing all the parameters for the str_replace() function. If that is not enough, place the cursor inside the str_replace function name and press Shift + F1 (Mac: shift + F1). You will get the manual for the function.

If you want to test the next operation, open the project created in the Quick start – your first PHP application section and place the cursor inside the class name Controller in the src/My/HelloBundle/Controller/DefaultController.php file. The place where you should place the cursor is denoted with bar | in the following code:

class DefaultController extends Cont|roller { }

The Quick Definition operation will show you the class definition:

The Quick Documentation operation will show you the documentation defined with PhpDoc blocks:

It is a formal standard for commenting on the PHP code. The official documentation is available at http://www.phpdoc.org.

Generators

PhpStorm enables you to do the following:

  • Implement magic methods

  • Override inherited methods

  • Generate constructor, getters, setters, and docblocks

All of these operations are available in Code | Generate (Alt + Insert, Mac: command + N). Perform the following steps:

  1. Create a new class Foo and place the cursor at the position of | :

    class Person { | }

  2. The Generate dialog box will contain the following operations:

    The Implement Methods dialog box contains all available magic methods:

  3. Create the class with two private properties:

    class Lorem { private $ipsum; private $dolor; }

  4. Then go to Code | Generate | Getters and Setters. In the dialog box select both properties:

  5. Then press OK. PhpStorm will generate the following methods:

    class Lorem { private $ipsum; private $dolor; public function setDolor($dolor) { $this->dolor = $dolor; } public function getDolor() { return $this->dolor; } public function setIpsum($ipsum) { $this->ipsum = $ipsum; } public function getIpsum() { return $this->ipsum; } }

  6. Next, go to Code | Generate | DocBlocks and in the dialog box select all the properties and methods:

  7. PhpStorm will generate docblocks for each of the selected properties and methods, for example:

    /** * @param $dolor */public function setDolor($dolor) { $this->dolor = $dolor; }

Summary

We just learned that in some cases you don’t have to type the code at all, as it can be generated automatically. Generators discussed in this article lifted the burden of type setters, getters and magic functions from your shoulders. We also dived into different ways to access documentation here.

Resources for Article :


Further resources on this subject:


Packt

Share
Published by
Packt

Recent Posts

Top life hacks for prepping for your IT certification exam

I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…

3 years ago

Learn Transformers for Natural Language Processing with Denis Rothman

Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…

3 years ago

Learning Essential Linux Commands for Navigating the Shell Effectively

Once we learn how to deploy an Ubuntu server, how to manage users, and how…

3 years ago

Clean Coding in Python with Mariano Anaya

Key-takeaways:   Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…

3 years ago

Exploring Forms in Angular – types, benefits and differences   

While developing a web application, or setting dynamic pages and meta tags we need to deal with…

3 years ago

Gain Practical Expertise with the Latest Edition of Software Architecture with C# 9 and .NET 5

Software architecture is one of the most discussed topics in the software industry today, and…

3 years ago