PhpStorm 2018.1 Help

Extract Field

The Extract Field refactoring lets you declare a new field and initialize it with the selected expression. The original expression is replaced with the usage of the field.

  1. Place the cursor within a piece of code you want to extract into a field.
  2. Press Ctrl+Alt+F or on the main menu, select Refactor | Extract Field.
  3. Select an expression you want introduce as a field.
    ps extract field
    If PhpStorm detects more than one occurrence in your code, it lets you specify which occurrences to replace.
    ps extract field occurrences
  4. Provide the name of the new field and choose where it will be initialized - in its declaration or in the current method.
    ps extract field init

    The new field is created with the visibility modifier that is set on the Code Generation tab of the Code Style. PHP page of the Settings/Preferences dialog (Ctrl+Alt+S).

Extracting a field using the dialog box

If the Enable in place refactorings checkbox is cleared on the Editor settings, the Extract Field refactoring is performed by means of the Extract Field Dialog.

  1. In the editor, select the expression or variable to be replaced with a field, or just place the cursor within such an expression or variable declaration.
  2. In the main menu, or the context menu of the selection, choose Refactor | Extract | Field, or press Ctrl+Alt+F.
  3. In the Extract Field Dialog dialog that opens:
    1. Specify the name of the field.
    2. Specify where the new field should be initialized by selecting the necessary option under Initialize in.
    3. In the Visibility area, select the visibility scope for the new field.
    4. To replace all the occurrences of the selected expression (if the selected expression is found more than once in the class), select the Replace all occurrences checkbox.
    5. Click OK.


Let's extract the 'param_query' argument into a $query class field. As a result, PhpStorm declares the new public static $query field and changes all 'param_query' occurrences to self::$query.

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']); } }
public static $query = 'param_query'; public static function find($params){ if (isset($params[self::$query])) { $result = MyDatabase::execute($params[self::$query]); } } public static function findAll($params){ if(isset($params[self::$query])){ $result = MyDatabase::executeAll($params[self::$query]); } }
Last modified: 19 May 2018

See Also