← Back to PHP Course | Chapter 8: OOP | Lesson 6 of 14

PHP Method Overriding

Method overriding is when a child does the same job in its own way, like a kid who is taught to tie shoes one way and decides to do it differently. The child's version replaces the parent's.
Syntax
php
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
<?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();
?>

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
<?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();
?>

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
<?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();
?>

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
<?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");
?>

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
<?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";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Changing the parameter list of an overriding method so it is no longer compatible with the parent's signature.
  2. Forgetting parent::method() when the parent's behavior should still run.
  3. Trying to override a final method, which is a fatal error.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.