PhpStorm 2021.2 Help

Extract constant

The Extract Constant refactoring makes your source code easier to read and maintain. It also helps you avoid using hardcoded constants without any explanations about their values or purpose.

Extract a PHP constant in-place

The in-place refactorings are enabled in PhpStorm by default. If you haven't changed this setting, the Extract Constant refactoring for PHP is performed in place, right in the editor.

  1. Place the caret within the expression or declaration of a variable to be replaced by a constant.

  2. Do one of the following:

    • Press Ctrl+Alt+C.

    • Choose Refactor | Extract | Constant from the main menu or the context menu.

  3. If more than one expression is detected for the current caret position, the Expressions list appears. If this is the case, click the expression to select it. Alternatively, press Up or Down to navigate to the expression of interest, and then press Enter to select it.

    Select expression
  4. If more than one occurrence of the expression is found, specify whether you wish to replace only the selected occurrence, or all the found occurrences with the new constant.

  5. If you want the constant to be defined in a different class, select the Move to another class checkbox.

    Move constant to another class
  6. Specify the name of the constant. Select the name from the list or type the name in the box with a red border.

  7. To complete the refactoring, press Tab or Enter.

    If you haven't completed the refactoring and want to cancel the changes you have made, press Escape.

  8. If you have selected to move the extracted constant definition to a different class, the Extract Constant Refactoring dialog opens. In the dialog, select the Target class checkbox and type the desired class name in the field.

Extract a constant using the dialog

If the in-place refactorings are disabled, the Extract Constant refactoring is performed by means of the Extract Constant Dialog dialog.

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

  2. Press Ctrl+Alt+C or select Refactor | Extract | Constant from the main menu or the editor context menu.

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

  4. In the Extract Constant dialog, type the name of the new constant in the Name field.

  5. 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.

  6. 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 of the Settings/Preferences dialog Ctrl+Alt+S.

    Otherwise, if the refactoring is invoked outside a PHP class definition, choose the method to define the PHP constant in the Constant syntax area.

    • To have PhpStorm define the constant through the define() function, choose define.

    • 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.

  7. If you want the constant to be defined in a different class, select the Target class checkbox and type the desired class name in the field.

  8. Click OK to start the refactoring.

PHP examples

Extracting a class constant

When a constant is extracted within a class definition, the new constant is defined through the const keyword and referenced through the self keyword.

In PHP language version 7.1 and later, you can additionally mark the extracted constant as public, private, or protected.

Before

After

class const_extraction { public static function find($params) { if (isset($params['param_query'])) { $result = MyDatabase::execute($params['param_query']); } } public static function findAll($params) { if (isset($params['param_query'])) { $result = MyDatabase::executeAll($params['param_query']); } } }
class const_extraction { const PARAM_QUERY = 'param_query'; public static function find($params) { if (isset($params[self::PARAM_QUERY])) { $result = MyDatabase::execute($params[self::PARAM_QUERY]); } } public static function findAll($params) { if(isset($params[self::PARAM_QUERY])) { $result = MyDatabase::executeAll($params[self::PARAM_QUERY]); } } }

Extracting a constant outside a class

When a constant is extracted outside a class definition, you can choose whether it will be defined through the const keyword or through the define() function.

Before

After

function find($params) { if (isset($params['param_query'])) { $result = MyDatabase::execute($params['param_query']); } } function findAll($params) { if (isset($params['param_query'])) { $result = MyDatabase::executeAll($params['param_query']); } }

const definition:

const PARAM_QUERY = 'param_query'; function find($params) { if (isset($params[PARAM_QUERY])) { $result = MyDatabase::execute($params[PARAM_QUERY]); } } function findAll($params) { if (isset($params[PARAM_QUERY])) { $result = MyDatabase::executeAll($params[PARAM_QUERY]); } }

define() definition:

define('PARAM_QUERY', 'param_query'); function find($params) { if(isset($params[PARAM_QUERY])) { $result = MyDatabase::execute($params[PARAM_QUERY]); } } function findAll($params) { if(isset($params[PARAM_QUERY])) { $result = MyDatabase::executeAll($params[PARAM_QUERY]); } }

JavaScript example

Before

After

Parenizor.method('toString', function () { return '(' + this.getValue() + ')'; })
Parenizor.method('toString', function () { const string = '(' + this.getValue() + ')'; return string; })
Last modified: 22 November 2021