0
0
PhpHow-ToBeginner · 4 min read

How to Override Method in PHP: Simple Guide with Examples

In PHP, you override a method by defining a method with the same name in a child class that extends a parent class. Use class Child extends Parent and then declare the method in the child class to replace the parent's version.
📐

Syntax

To override a method in PHP, create a child class that extends a parent class and define a method with the same name in the child class. This new method replaces the parent's method when called on the child object.

  • class Child extends Parent: declares inheritance.
  • function methodName(): defines the method to override.
php
<?php
class ParentClass {
    public function greet() {
        echo "Hello from Parent!\n";
    }
}

class ChildClass extends ParentClass {
    public function greet() {
        echo "Hello from Child!\n";
    }
}
?>
💻

Example

This example shows a parent class with a greet method and a child class that overrides it. When calling greet on the child object, the child's version runs instead of the parent's.

php
<?php
class ParentClass {
    public function greet() {
        echo "Hello from Parent!\n";
    }
}

class ChildClass extends ParentClass {
    public function greet() {
        echo "Hello from Child!\n";
    }
}

$parent = new ParentClass();
$parent->greet();  // Outputs: Hello from Parent!

$child = new ChildClass();
$child->greet();   // Outputs: Hello from Child!
?>
Output
Hello from Parent! Hello from Child!
⚠️

Common Pitfalls

Common mistakes when overriding methods in PHP include:

  • Not matching the method name exactly (case-sensitive).
  • Changing method visibility to a more restrictive level (e.g., from public to private), which causes errors.
  • Forgetting to call the parent method if needed using parent::methodName().

Always keep method signatures compatible and visibility the same or less restrictive.

php
<?php
// Wrong: changing visibility to private causes error
class ParentClass {
    public function greet() {
        echo "Hello from Parent!\n";
    }
}

class ChildClass extends ParentClass {
    // This will cause a fatal error
    private function greet() {
        echo "Hello from Child!\n";
    }
}

// Correct way: keep visibility public
class ChildClassCorrect extends ParentClass {
    public function greet() {
        echo "Hello from Child!\n";
    }
}
?>
📊

Quick Reference

Summary tips for method overriding in PHP:

  • Use extends to inherit from a parent class.
  • Define a method with the same name in the child class to override.
  • Keep method visibility the same or less restrictive.
  • Call parent::methodName() inside the child method to use the parent's version if needed.

Key Takeaways

Override methods by defining them with the same name in a child class that extends a parent class.
Keep method visibility the same or less restrictive when overriding to avoid errors.
Use parent::methodName() inside the child method to call the original parent method if needed.
Method names are case-sensitive and must match exactly to override properly.
Overriding allows customizing or extending behavior of inherited methods in PHP.