PhpStorm 2024.3 Help

Make Static

The Make Static refactoring converts an instance method to a static one and automatically corrects all calls, implementations and overridings of the method.

  1. Select the method to be refactored in the Structure view, or right-click the method name in the editor. From the main menu or the context menu of the selection, choose Refactor | Make Static. If your method does not need any parameters, nor relies on accessing properties or methods of this object, you will get the static method made silently, without displaying any dialogs:

    class MyClass { private function getFormattedDate() { $format = getSettings()['dateFormat']; return time($format); } }
    class MyClass { private static function getFormattedDate() { $format = getSettings()['dateFormat']; return time($format); } }

    For more complicated cases, the Make Method Static dialog opens.

  2. In the Make Method Static dialog, do one of the following:

    • If you currently use the existing object via $this, select the Add object as a parameter with name checkbox to pass in an instance of the object via a parameter. In the field below, specify the name of the parameter to be generated. After the refactoring, the new parameter will be documented in the PHPDoc block.

      class MyClass { private function getFormattedDate() { $format = $this->getSettings()['dateFormat']; return time($format); } }
      class MyClass { /** * @param MyClass $instance * @return */ private static function getFormattedDate($instance) { $format = $instance->getSettings()['dateFormat']; return time($format); } }
    • If you access the properties of the class, use the Add parameters for properties area to pass the value of the property as a parameter instead of accessing the object inside the newly created static method.

      1. Select the Add parameters for properties checkbox.

      2. In the Parameters list, which shows all the possibly suggested parameters, select the checkboxes next to the ones that you want to pass the values in.

      class MyClass { private function getFormattedDate() { $format = $this->timeFormat; return time($format); } }
      class MyClass { /** * @param $timeFormat * @return int; */ private static function getFormattedDate($timeFormat) { $format = timeFormat; return time($format); } }
  3. To preview the results, click Preview and examine the result of the refactoring in the Find tool window. Apply the changes if no issues arise.

Last modified: 16 August 2024