PHP Method Overriding: Complete Guide with Examples

1. Introduction

In PHP, method overriding occurs when a child class defines a method with the same name and parameters as a method in its parent class. When this happens, the child class method replaces the parent class method.

Method overriding is mainly used to achieve runtime polymorphism,

📖
Key points:
  • The child class must have a method with the same name as the parent class method.
  • The parameters should match the parent method.
  • The child class method overrides the parent class method.
  • Method overriding works with inheritance.
  • Static methods cannot be overridden in PHP.

Note: Method overriding allows developers to customize or extend the functionality of existing classes without modifying the original code.

2. Syntax

The syntax for method overriding in PHP is straightforward. The child class extends the parent class and defines a method with the same name.


class ParentClass {

   public function methodName() {
       // parent method code
   }

}

class ChildClass extends ParentClass {

   public function methodName() {
       // overridden method code
   }

}

Explanation:

  • ParentClass contains the original method.
  • ChildClass extends the parent class using the extends keyword.
  • The method inside the child class overrides the parent method.

When the method is called using the child object, the child method executes instead of the parent method.

3. Example

Below is a simple example of PHP method overriding.



class Company {

   public function fresherPackage() {
       echo "No package defined.";
   }

}

class TCS extends Company {

   public function fresherPackage() {
       echo "TCS provides 3.2 Lakh per annum.";
   }

}

class Oracle extends Company {

   public function fresherPackage() {
       echo "Oracle provides 4.2 Lakh per annum.";
   }

}

$tcs_Obj = new TCS();
$tcs_Obj->fresherPackage();

echo "<br>";

$oracle_Obj = new Oracle();
$oracle_Obj->fresherPackage();

In this example, the fresherPackage() method in the child classes overrides the method defined in the parent class.

Output:

TCS provides 3.2 Lakh per annum.
Oracle provides 4.2 Lakh per annum.

Explanation:

Let’s understand how method overriding works step by step.

First, we create a parent class called Company.


class Company
//Inside this class, we define the method fresherPackage().
public function fresherPackage() {
   echo "No package defined.";
}

Next, we create a child class called TCS using the extends keyword.


class TCS extends Company
//Inside this class, we define the same method again.
public function fresherPackage() {
   echo "TCS provides 3.2 Lakh per annum.";
}

This method replaces the parent method for objects of the TCS class.

Similarly, we create another child class, Oracle.


class Oracle extends Company {

   public function fresherPackage() {
       echo "Oracle provides 4.2 Lakh per annum.";
   }

}

This class also overrides the fresherPackage() method with its own implementation.

When we create objects:


$tcs_Obj = new TCS();
$oracle_Obj = new Oracle();
//And call the method:
$tcs_Obj->fresherPackage();
$oracle_Obj->fresherPackage();

PHP automatically calls the child class method instead of the parent method.

This is known as runtime polymorphism because the method executed depends on the object created at runtime.

4. Real-world Example

Let’s look at a practical example using an Employee Salary System.

Different employee roles may calculate salary differently, even though they share a common structure.



class Employee {

   public function calculateSalary() {
       echo "Basic salary calculation.";
   }

}

class Developer extends Employee {

   public function calculateSalary() {
       echo "Developer salary includes coding allowance.";
   }

}

class Manager extends Employee {

   public function calculateSalary() {
       echo "Manager salary includes management bonus.";
   }

}

$dev = new Developer();
$dev->calculateSalary();

echo "<br>";

$mgr = new Manager();
$mgr->calculateSalary();

In this example:

  1. The Employee class defines a general salary calculation.
  2. The Developer and Manager classes override that method with their own logic.

This type of implementation is commonly used in real-world business applications.

Output

Developer salary includes coding allowance.
Manager salary includes management bonus.

Each child class produces different output even though the method name is the same.

5. Conclusion

PHP method overriding is an important concept in Object-Oriented Programming that allows child classes override methods of parent class.

Method overriding works together with inheritance and polymorphism to create flexible and scalable applications. It allows the same method name to behave differently depending on the object that calls it.

PHP Method Overriding – Interview Questions

Q 1: What is method overriding in PHP?
Ans: Method overriding occurs when a child class provides its own implementation of a method already defined in the parent class.
Q 2: Which concept is required for method overriding?
Ans: Inheritance is required.
Q 3: Can access modifiers be changed while overriding a method?
Ans: Yes, but visibility cannot be more restrictive than the parent method.
Q 4: What keyword is used to call the parent method?
Ans: The parent keyword.
Q 5: Why is method overriding useful?
Ans: It allows child classes to customize or extend parent class behavior.

PHP Method Overriding – Objective Questions (MCQs)

Q1. Method overriding occurs when ______.






Q2. Which access level allows overriding?






Q3. Method overriding is an example of ______.






Q4. Can a final method be overridden?






Q5. Parent class method can be accessed inside overriding method using ______.






Related PHP Tutorials