PHP Method Overriding
In this page:
class ParentClass {
function method_name() { }
}
class ChildClass extends ParentClass {
function method_name() {
parent::method_name(); // optional: call the parent version
}
}
What is Method Overriding?
Method overriding happens when a child class redefines a method that its parent class already has, replacing the parent's behavior with its own version when called on a child object.
Example: What is Method Overriding?
<?php
// Define the class `Animal`
class Animal {
// Define the function `speak` with no parameters
function speak() {
// Print "Some sound" to the output
echo "Some sound";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the function `speak` with no parameters
function speak() {
// Print "Bark!" to the output
echo "Bark!";
}
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak();
?>
Login to try C/C++/Java/PHP code in the editor
The parent Keyword
PHP resolves which version of an overridden method runs based on the actual object's class at runtime, not the type of variable holding it, which is what allows polymorphic code to treat different subclasses uniformly.
Example: The parent Keyword
<?php
// Define the class `Shape`
class Shape {
// Define the function `area` with no parameters
function area() {
// Return `0` from this function
return 0;
}
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
// Define the function `area` with no parameters
function area() {
// Return `4` from this function
return 4;
}
}
// Create a new `Square` instance, stored in `$shape`
$shape = new Square();
// Print `$shape->area()` to the output
echo $shape->area();
?>
Login to try C/C++/Java/PHP code in the editor
Overriding Constructors
parent::methodName() inside an overriding method lets you call the parent's original implementation and extend it, rather than replacing it entirely — useful when the child needs to add behavior before or after the inherited logic.
Example: Overriding Constructors
<?php
// Define the class `Animal`
class Animal {
// Define the function `speak` with no parameters
function speak() {
// Print "Generic sound\n" to the output
echo "Generic sound\n";
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the function `speak` with no parameters
function speak() {
parent::speak();
// Print "Bark!" to the output
echo "Bark!";
}
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak();
?>
Login to try C/C++/Java/PHP code in the editor
Preventing Overriding with final
An overriding method's signature (parameter types and visibility) generally needs to stay compatible with the parent's, and PHP will raise errors for certain incompatible changes, especially with typed parameters.
Example: Preventing Overriding with final
<?php
// Define the class `Animal`
class Animal {
// Define the function `speak` taking `string $sound`
function speak(string $sound) {
// Print `$sound` to the output
echo $sound;
}
}
// Define the class `Dog`, inheriting from `Animal`
class Dog extends Animal {
// Define the function `speak` taking `string $sound`
function speak(string $sound) {
// Print `strtoupper($sound)` to the output
echo strtoupper($sound);
}
}
// Create a new `Dog` instance, stored in `$dog`
$dog = new Dog();
$dog->speak("bark");
?>
Login to try C/C++/Java/PHP code in the editor
Overriding on cookiescursor.com
Overriding is central to polymorphism: a Shape base class might define a generic area() method that Circle and Square each override with their own correct formula, letting calling code just call ->area() without caring which shape it is.
Example: Overriding on cookiescursor.com
<?php
// Define the class `Shape`
class Shape {
// Define the function `area` with no parameters
function area() {
// Return `0` from this function
return 0;
}
}
// Define the class `Circle`, inheriting from `Shape`
class Circle extends Shape {
// Declare the private property `$radius`
private $radius;
// Define the constructor `__construct` taking `$radius`
function __construct($radius) {
$this->radius = $radius;
}
// Define the function `area` with no parameters
function area() {
// Return `3.14 * $this->radius * $this->radius` from this function
return 3.14 * $this->radius * $this->radius;
}
}
// Define the class `Square`, inheriting from `Shape`
class Square extends Shape {
// Declare the private property `$side`
private $side;
function __construct($side) {
$this->side = $side;
}
function area() {
return $this->side * $this->side;
}
}
$shapes = [new Circle(2), new Square(3)];
foreach ($shapes as $shape) {
echo $shape->area() . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- Changing the parameter list of an overriding method so it is no longer compatible with the parent's signature.
- Forgetting
parent::method()when the parent's behavior should still run. - Trying to override a
finalmethod, which is a fatal error.
Chapter Quiz — Complete all 14 topics to unlock
0/14 topics done
Complete these topics first: