PhpStorm 2024.2 Help

Extract constant

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

  1. In the editor, select an expression or declaration of a variable you want to replace with a constant.

  2. Press Ctrl+Alt+C to introduce a constant or select Refactor | Extract/Introduce | Constant in the main menu.

    Alternatively, on the toolbar that appears, click Extract and select Constant.

    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.

  3. Accept the proposed constant name or type your own name.

    If required, select the Move to another class checkbox or 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.

  4. Press Tab or Enter to complete the refactoring.

    Extract constant
  5. If you've selected the Move to another class checkbox, an Extract Constant dialog will be displayed to prompt you specify the target class.

    Extract constant dialog

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: 08 October 2024