Pull Members Up
Basics
The Pull Members Up refactoring allows you to move class members to a parent class. This may be helpful when you start to add functionality from the bottom of the class hierarchy and then realize that it can be also used in more common cases (otherwise some parts of your code may become obsolete for the parent class but still be valid for one of its children). With the Pull members up refactoring, you no longer need to manually copy a method or field from one class, paste it into another and fix internal references in the member.
Pulling members up
- Select the class to be moved to a parent class.
- On the main menu or on the context menu, choose . The Pull Members Up dialog box appears.
- Select the destination object (parent class).
- In the Members section, select the members you want to move.
- Click Refactor to pull the selected members to their destination.
PHP example
Suppose you have a class Car
which extends the parent class Vehicle
:
abstract class Vehicle {
}
class Car extends Vehicle {
protected $weight;
private $numOfPassengers;
function __construct($weight,$numOfPassengers) {
$this->weight = $weight;
$this->numOfPassengers = $numOfPassengers;
}
protected function printWeight() {
echo 'Weight = ' . $this->weight;
}
protected function printPassengers() {
echo 'Number of passengers = ' . $this->numOfPassengers;
}
}
printPassengers()
and the field $numOfPassengers
from Car
to Vehicle
. - Place the caret inside the class
Car
and choose on the context menu. - In the Pull Members Up Dialog that opens, select the check boxes next to
printPassengers()
and$numOfPassengers
. PhpStorm informs you that the visibility of$numOfPassengers
will be changed from private to protected. - Click Refactor. After the refactoring your classes will look as follows:
abstract class Vehicle { protected $numOfPassengers; protected function printPassengers() { echo 'Number of passengers = ' . $this->numOfPassengers; } } class Car extends Vehicle { protected $weight; function __construct($weight,$numOfPassengers) { $this->weight = $weight; $this->numOfPassengers = $numOfPassengers; } protected function printWeight() { echo 'Weight = ' . $this->weight; } }
See Also
Procedures:
Reference:
Last modified: 19 July 2017