How to extract a constant

In the editor, position the cursor inside the expression to be replaced with a constant.

Choose Refactor | Extract Constant on the main menu or on the context menu of the selection. Alternatively pressCtrl+Alt+C.

If a constant can be extracted from several expressions in the current context, PhpStorm shows all the relevant expressions in a pop-up list. Select the expression to apply the refactoring to.

In the Extract Constant dialog box, type the name of the new constant in the Name text box.

Specify the scope to apply refactoring in:

To have only the selected expression replaced, clear the Replace all occurrences checkbox.

To have PhpStorm replace the selected expression wherever it is used, select the Replace all occurrences checkbox.

If the refactoring is invoked inside a class definition, choose the visibility scope (access level modifier) of the extracted constant. If you leave the default value selected, the constant will be implicitly defined as public without any modifier applied to it. Otherwise, you can choose the appropriate option to explicitly mark the constant as public, private, or protected.

Setting visibility scope is only possible in PHP language version 7.1 and later. You can specify the PHP language level on the PHP page (File | Settings | Languages and Frameworks | PHP for Windows and Linux or PhpStorm | Preferences | Languages and Frameworks | PHP for macOS).

To have PhpStorm define the constant through the const keyword outside a class definition, choose const.

This method preserves the approach accepted in PHP version 5.3.0. Note that such constants are defined during compilation therefore they should be declared at the top-level scope but not inside functions, loops or if statements.

Click OK to start the refactoring.

If the refactoring is applied inside a class definition, it will result in extracting a class constant that is always defined through the const keyword, therefore the choice is irrelevant.