Please wait

Overriding Methods

Another aspect of inheritance is the ability to override methods. Method overriding in PHP is an important feature of Object-Oriented Programming (OOP), where a child class provides a different implementation of a method that is already defined in its parent class. This allows the child class to inherit methods from the parent class but also change their behavior if needed.

There are a few things to keep in mind when overriding methods. The method present in the parent class is referred to as the 'overridden' method, and the method in the child class that alters it is known as the 'overriding' method. The implementation in the overriding method supersedes that of the overridden method.

The specific method (overridden or overriding) that PHP opts to execute is determined by the type of object that triggers the method.

  • If the method is invoked by an object of the parent class, PHP executes the overridden method from the parent class.
  • Conversely, if the method is called by an object of the child class, PHP carries out the overriding method from the child class.

Overriding a parent method

Consider a Vehicle class that has a method startEngine():

class Vehicle {
  public function startEngine() {
    echo "Starting the vehicle's engine.\n";
  }
}

Now, suppose we have a Car class that extends Vehicle. The Car class inherits the startEngine() method from Vehicle. But let's say that starting the engine in a car is a bit different from starting the engine in a general vehicle. We can provide a different implementation of startEngine() in the Car class:

class Car extends Vehicle {
  public function startEngine() {
    echo "Starting the car's engine. Make sure the gear is in neutral.\n";
  }
}

Now, when you call startEngine() on an instance of Car, it will use the startEngine() method defined in the Car class, not the one from Vehicle:

$myCar = new Car();
$myCar->startEngine();  // Outputs: Starting the car's engine. Make sure the gear is in neutral.

This is method overriding. The Car class has overridden the startEngine() method from the Vehicle class.

Running an overridden method

Even when a method has been overridden in a child class, it is possible to call the parent's version of that method using the parent keyword followed by the scope resolution operator (::) and the method name. This is a common practice when the child's version of the method extends the behavior of the parent's version rather than replacing it entirely.

class Vehicle {
  public function startEngine() {
    echo "Starting the vehicle's engine.\n";
  }
}
 
class Car extends Vehicle {
  public function startEngine() {
    // Do something specific to cars first
    echo "Make sure the gear is in neutral.\n";
 
    // Then call the parent's version of the method
    parent::startEngine();
  }
}
 
$myCar = new Car();
$myCar->startEngine();
// Outputs:
// Make sure the gear is in neutral.
// Starting the vehicle's engine.

In this example, when you call startEngine() on an instance of Car, it first executes the code specific to the Car class, then uses parent::startEngine() to call the Vehicle version of startEngine(). This allows the Car version of startEngine() to extend the behavior of the Vehicle version rather than replacing it completely.

Final keyword

The final keyword can be used to prevent method overriding or class extension. This is particularly useful when you want to restrict the alteration of some functionality in child classes, ensuring the integrity of your code's behavior.

  • When applied to a method: If you declare a method in the parent class as final, it cannot be overridden in any child class.
  • When applied to a class: If you declare a class as final, it cannot be extended by any other class.

Here is an example of how to use the final keyword with methods:

class Vehicle {
  final public function startEngine() {
    echo "Starting the vehicle's engine.\n";
  }
}
 
class Car extends Vehicle {
  // This would raise an error, because startEngine() is declared final in Vehicle
  public function startEngine() {
    echo "Starting the car's engine.\n";
  }
}

In this example, declaring startEngine() as final in the Vehicle class means it cannot be overridden in the Car class. If you try to do so, PHP will raise a fatal error.

And here is an example of how to use the final keyword with classes:

final class Vehicle {
    public function startEngine() {
        echo "Starting the vehicle's engine.\n";
    }
}
 
// This would raise an error because Vehicle is declared final
class Car extends Vehicle {
    public function startEngine() {
        echo "Starting the car's engine.\n";
    }
}

In this example, declaring Vehicle as a final class means it cannot be extended. The attempt to extend Vehicle with Car results in a fatal error.

Key Takeaways

  • Method Overriding is an Object-Oriented Programming (OOP) feature in PHP where a child class provides a different implementation of a method that is already defined in its parent class.
  • The method in the parent class is called the 'overridden' method, while the method in the child class is known as the 'overriding' method.
  • Overriding allows a child class to change the behavior of a method inherited from the parent class. The new method must have the same name and number/type of parameters.
  • If the child class does not provide an overriding method, the parent class's method is used. If the child class does provide an overriding method, that is used instead.
  • To call a parent's overridden method from within a child class, you can use the parent::methodName() syntax.
  • The final keyword can be used to prevent a method from being overridden in child classes. If a method is declared as final, any attempt to override that method in a child class will result in a fatal error.
  • Similarly, declaring a class as final will prevent it from being extended, i.e., no class can inherit from a final class.

Comments

Please read this before commenting