Inheritance in PHP allows one class to inherit the properties and methods of another class. It is a key concept in Object-Oriented Programming (OOP) and provides code reusability, allowing you to build on existing classes without rewriting code.
In this tutorial, we will cover:
Let’s explore each of these concepts with examples and explanations.
1. What is Inheritance?
Inheritance allows a class (child or derived class) to inherit the properties and methods of another class (parent or base class). The child class can reuse or override the properties and methods from the parent class, adding its own behavior or modifying existing behavior.
Key Benefits of Inheritance:
- Code Reusability: You can reuse code across multiple classes by defining common functionality in a parent class.
- Extensibility: You can add new functionality to child classes without modifying the parent class.
2. Defining a Parent Class
A parent class (also called a base or superclass) is a class that contains properties and methods that can be inherited by child classes.
Example (Defining a Parent Class):
<?php // Define a parent class class Animal { public $name; // Constructor to initialize the name public function __construct($name) { $this->name = $name; } // Method to describe the animal public function describe() { echo "This is a " . $this->name . "\n"; } } ?>
- In this example, the Animal class defines a property $name and a method describe(). These can be inherited by child classes.
3. Defining a Child Class
A child class (also called a subclass or derived class) extends the parent class and inherits its properties and methods. In PHP, you define a child class using the extends keyword.
Example (Defining a Child Class):
<?php // Define a child class that inherits from Animal class Dog extends Animal { // Method specific to Dog class public function makeSound() { echo "Bark\n"; } } // Instantiate a Dog object $dog = new Dog("Dog"); $dog->describe(); // Outputs: This is a Dog $dog->makeSound(); // Outputs: Bark ?>
- In this example, the Dog class inherits from the Animal class. It has access to the describe() method from the parent class and defines its own makeSound() method.
4. Overriding Parent Methods
A child class can override a method from the parent class by defining a method with the same name. When the child class method is called, it will replace the parent class method.
Example (Overriding a Parent Method):
<?php class Cat extends Animal { // Override the describe method public function describe() { echo "This is a cat named " . $this->name . "\n"; } // Method specific to Cat class public function makeSound() { echo "Meow\n"; } } // Instantiate a Cat object $cat = new Cat("Kitty"); $cat->describe(); // Outputs: This is a cat named Kitty $cat->makeSound(); // Outputs: Meow ?>
- In this example, the Cat class overrides the describe() method from the Animal class and provides its own implementation.
5. Using parent:: to Access Parent Methods
You can use parent:: to access a parent class’s methods or properties from within a child class. This is useful when you want to extend a method but still use the behavior defined in the parent class.
Example (Using parent::):
<?php class Bird extends Animal { // Override the describe method and call the parent method public function describe() { parent::describe(); // Call the parent method echo "It has wings and can fly.\n"; } public function makeSound() { echo "Chirp chirp\n"; } } // Instantiate a Bird object $bird = new Bird("Bird"); $bird->describe(); // Outputs: This is a Bird, followed by "It has wings and can fly." $bird->makeSound(); // Outputs: Chirp chirp ?>
- In this example, the Bird class overrides the describe() method but still calls the parent’s describe() method using parent::describe().
6. Protected Members and Inheritance
In PHP, class members (properties and methods) can be public, protected, or private:
- Public: Accessible from anywhere.
- Protected: Accessible within the class and its child classes, but not from outside the class.
- Private: Accessible only within the class itself.
Example (Using Protected Members):
<?php class Animal { protected $name; public function __construct($name) { $this->name = $name; } protected function getName() { return $this->name; } } class Dog extends Animal { public function describe() { echo "This is a dog named " . $this->getName() . "\n"; } } // Instantiate a Dog object $dog = new Dog("Buddy"); $dog->describe(); // Outputs: This is a dog named Buddy ?>
- The $name property and getName() method are protected, meaning they are accessible in the Dog class but not from outside the Animal class.
7. Inheritance with Constructors
When a child class inherits from a parent class, the parent class constructor is not called automatically. You must explicitly call it using parent::__construct().
Example (Calling Parent Constructor):
<?php class Animal { protected $name; public function __construct($name) { $this->name = $name; echo "Animal constructor called\n"; } } class Dog extends Animal { public function __construct($name) { parent::__construct($name); // Call the parent constructor echo "Dog constructor called\n"; } public function describe() { echo "This is a dog named " . $this->name . "\n"; } } // Instantiate a Dog object $dog = new Dog("Buddy"); // Outputs: // Animal constructor called // Dog constructor called $dog->describe(); // Outputs: This is a dog named Buddy ?>
- In this example, the Dog class constructor calls the parent’s constructor using parent::__construct($name).
8. Final Keyword
In PHP, the final keyword can be used to prevent classes or methods from being overridden:
- A final class cannot be extended.
- A final method cannot be overridden by child classes.
Example (Using the final Keyword):
<?php // Final class cannot be extended final class Animal { public function makeSound() { echo "Generic animal sound\n"; } } // Uncommenting the below code will cause an error // class Dog extends Animal { } class Cat { // Final method cannot be overridden final public function sleep() { echo "Cat is sleeping\n"; } } class PersianCat extends Cat { // This will cause an error because sleep() is final // public function sleep() { ... } } ?>
- The final keyword ensures that the Animal class cannot be extended and the sleep() method in Cat cannot be overridden.
9. Example Use Case: Employee and Manager Classes
Let’s use inheritance in a more practical example, such as an employee-management system where Manager inherits from Employee.
Example (Employee and Manager):
<?php // Parent class class Employee { protected $name; protected $salary; public function __construct($name, $salary) { $this->name = $name; $this->salary = $salary; } public function showDetails() { echo "Employee Name: " . $this->name . "\n"; echo "Salary: $" . $this->salary . "\n"; } } // Child class class Manager extends Employee { private $bonus; public function __construct($name, $salary, $bonus) { parent::__construct($name, $salary); $this->bonus = $bonus; } public function showDetails() { parent::showDetails(); // Call parent method echo "Bonus: $" . $this->bonus . "\n"; } } // Create instances $employee = new Employee("John Doe", 50000); $employee->showDetails(); // Outputs: // Employee Name: John Doe // Salary: $50000 $manager = new Manager("Jane Smith", 70000, 15000); $manager->showDetails(); // Outputs: // Employee Name: Jane Smith // Salary: $70000 // Bonus: $15000 ?>
- In this example, Manager extends Employee and adds additional functionality (bonus). Both classes have their own constructor and use parent::showDetails() to reuse the Employee method.
Summary of PHP Inheritance:
Concept | Description |
---|---|
extends | Used to define a child class that inherits from a parent class |
parent:: | Used to access methods and properties of the parent class |
Overriding | Child classes can override methods from the parent class |
Protected Members | Accessible within the class and its child classes |
final | Prevents a class from being extended or a method from being overridden |
Constructor Inheritance | Child constructors can call parent constructors using parent::__construct() |
Conclusion
Inheritance in PHP is a powerful feature of object-oriented programming that allows you to build scalable and reusable code. In this tutorial, we covered:
- Defining parent and child classes.
- Overriding methods in child classes and using the parent:: keyword.
- Working with protected members and constructors in inherited classes.
- The final keyword to prevent overriding or extending.
- A practical use case with Employee and Manager classes.