Welcome back! Now that you have a solid understanding of classes and objects in PHP, it's time to build on that knowledge by exploring inheritance. Consider it a natural progression in our journey into object-oriented programming (OOP).
Inheritance allows you to create a new class based on an existing class. By using inheritance, you can reuse code, add new features, and make your programs easier to manage and understand. Let's dive in and see what it's all about.
In this lesson, you'll understand how to use inheritance in PHP. We'll cover:
- What Inheritance Is
- How to Implement Inheritance in PHP
- Why Inheritance Is Beneficial
Inheritance is a way to establish a relationship between a new class (child class) and an existing class (parent class). The child class inherits properties and behaviors (methods) from the parent class.
Here’s a simple example:
php1<?php 2 3// Define the base class Person with name and age attributes 4class Person { 5 // Declare properties 6 private $name; 7 private $age; 8 9 // Constructor to initialize name and age 10 public function __construct($name, $age) { 11 $this->name = $name; 12 $this->age = $age; 13 } 14 15 // Display method to show name and age 16 public function display() { 17 echo "Name: " . $this->name . ", Age: " . $this->age . "\n"; 18 } 19 20 // Greet method to display a greeting message 21 public function greet() { 22 echo "Hello, there!\n"; 23 } 24} 25 26// Define the derived class Student, inheriting from Person 27class Student extends Person { 28 // Declare property for major 29 private $major; 30 31 // Constructor to initialize name, age of the base class, and the major of the student 32 public function __construct($name, $age, $major) { 33 // Call parent constructor to initialize base attributes 34 parent::__construct($name, $age); 35 $this->major = $major; 36 } 37 38 // Display method to show name, age using the base class display method, and major of the student 39 public function display() { 40 $this->greet(); 41 parent::display(); 42 echo "Major: " . $this->major . "\n"; 43 } 44} 45 46// Create a Student object and display its details 47$student = new Student("Bob", 25, "Computer Science"); 48$student->display(); 49 50?>
In this snippet, the Student
class inherits from the Person
class. It reuses the name
and age
attributes and methods from the Person
class and adds a new attribute major
and a new display
method to show the student's major.
When you declare a child class, you specify the parent class it inherits from by using the extends
keyword. The child class can then extend or override the functionality of the parent class.
In our example:
- The
Person
class is the parent class. - The
Student
class is the child class, inheritingname
andage
from thePerson
class. - The
Student
class also adds a new member,major
, and overrides thedisplay
method to include information about the major.- Notice how
Student::display()
callsparent::display()
to reuse the parent class functionality before adding its details. - The
greet
method is also called from thedisplay
method to show how the child class can access parent class methods.
- Notice how
In PHP, the protected
access modifier plays a critical role in class inheritance. When you declare a class property or method as protected
, it means that it can be accessed within the class itself and by inheriting child classes. However, it cannot be accessed from outside these classes. This allows child classes to have controlled access to the parent class’s members.
Let's consider an example to illustrate the use of the protected
modifier:
php1<?php 2 3// Define the base class Animal with a protected property and method 4class Animal { 5 // Declare a protected property 6 protected $name; 7 8 // Constructor to initialize name 9 public function __construct($name) { 10 $this->name = $name; 11 } 12 13 // Protected method to describe the animal 14 protected function describe() { 15 echo "This is an animal named " . $this->name . ".\n"; 16 } 17} 18 19// Define the derived class Dog, inheriting from Animal 20class Dog extends Animal { 21 // Method to show details about the dog 22 public function showDetails() { 23 // Access the protected property and method from the parent class 24 echo "Dog's Name: " . $this->name . "\n"; 25 $this->describe(); 26 } 27} 28 29// Create a Dog object and display its details 30$dog = new Dog("Buddy"); 31$dog->showDetails(); 32 33?>
In this example:
- The
Animal
class has a protected property$name
and a protected methoddescribe()
. - The
Dog
class, which inherits fromAnimal
, is able to access the protected property$name
and the methoddescribe()
, showcasing the ability of theprotected
modifier to allow access within the child class. - Attempting to access
$dog->name
or$dog->describe()
outside of these classes will result in a fatal error, as they are protected members.
Using protected
allows encapsulation while still providing flexibility for inheritance, which enhances the design of object-oriented applications.
Inheritance is powerful for several reasons:
- Code Reusability: Instead of rewriting common functionalities, you can inherit them from a parent class, making maintenance easier and reducing errors.
- Extension: You can extend existing code by adding new features to a child class without changing the existing parent class.
- Hierarchy: It helps in organizing code in a hierarchical manner, which reflects real-world relationships and improves code readability and structure.
Inheritance is a cornerstone of OOP, and understanding it will enable you to design more flexible and scalable applications. It's an essential concept for mastering OOP.
Excited to start practicing? Let's move on and put this theory into action!