Hello again! In this lesson, we will explore inheritance in object-oriented programming (OOP) using PHP. Inheritance is a fundamental concept that allows us to share code between classes, thereby enhancing code reusability and efficiency.
We'll delve into inheritance by discussing attribute and method inheritance using practical PHP examples. Our lesson's blueprint will cover defining inheritance, examining attribute inheritance, exploring method inheritance, and understanding the usage of the parent class constructor in PHP. Ready? Let's get started!
Inheritance in PHP entails creating a derived class (child class) that inherits properties and methods from a base class (parent class). It's common to encounter situations where classes share common characteristics, making inheritance extremely useful.
Here's an example showcasing a base class Vehicle
and a derived class Car
:
php1<?php 2 3// Define the base class 'Vehicle' 4class Vehicle { 5 protected $color; 6 protected $brand; 7 8 // Constructor to initialize 'color' and 'brand' attributes 9 public function __construct($color, $brand) { 10 $this->color = $color; 11 $this->brand = $brand; 12 } 13} 14 15// Define the derived class 'Car' 16class Car extends Vehicle { // Car inherits from Vehicle 17 private $doors; 18 19 // Constructor to initialize 'color', 'brand', and 'doors' 20 public function __construct($color, $brand, $doors) { 21 parent::__construct($color, $brand); // Call the parent class constructor 22 $this->doors = $doors; 23 } 24} 25 26?>
We will focus primarily on single inheritance in PHP, where one base class is extended by one derived class.
With attribute inheritance, a derived class inherits the attributes of a base class, allowing it to make use of shared properties.
Consider an example with a base class Artist
and a derived class Musician
:
php1<?php 2 3// Define the base class 'Artist' 4class Artist { 5 protected $name; 6 7 // Constructor to initialize 'name' attribute 8 public function __construct($name) { 9 $this->name = $name; 10 } 11} 12 13// Define the derived class 'Musician' 14class Musician extends Artist { // Musician inherits from Artist 15 private $instrument; 16 17 // Constructor to initialize 'name' and 'instrument' attributes 18 public function __construct($name, $instrument) { 19 parent::__construct($name); // Call the parent constructor 20 $this->instrument = $instrument; 21 } 22 23 // Method to display the musician's details 24 public function display() { 25 echo "Name: " . $this->name . "\nInstrument: " . $this->instrument . "\n"; 26 } 27} 28 29// Create an instance of 'Musician' and display its details 30$john = new Musician("John Lennon", "Guitar"); 31$john->display(); 32// Name: John Lennon 33// Instrument: Guitar 34?>
The Musician
class inherits the name
attribute from the Artist
class and also possesses its unique attribute, instrument
.
Just like attributes, method inheritance allows a derived class to inherit methods from a base class.
In the following example, the Car
class inherits the start
method from the Vehicle
class:
php1<?php 2 3// Define the base class 'Vehicle' 4class Vehicle { 5 protected $brand; 6 7 // Constructor to initialize 'brand' attribute 8 public function __construct($brand) { 9 $this->brand = $brand; 10 } 11 12 // Method to simulate starting the vehicle 13 public function start() { 14 echo "The " . $this->brand . " is starting.\n"; 15 } 16} 17 18// Define the derived class 'Car' 19class Car extends Vehicle { // Car inherits from Vehicle 20 // Constructor to initialize 'brand' attribute 21 public function __construct($brand) { 22 parent::__construct($brand); 23 } 24} 25 26// Create an instance of 'Car' and call the inherited 'start' method 27$my_car = new Car("BMW"); 28$my_car->start(); // Output: The BMW is starting. 29?>
In PHP, the base class constructor can be called within the derived class constructor using the parent::__construct()
syntax. This ensures that a derived class can leverage or extend the functionality of a base class without altering it.
For example, when creating a derived class that adds new attributes, invoking the parent constructor ensures proper initialization, as shown below:
php1<?php 2 3// Define the base class 'ParentClass' 4class ParentClass { 5 protected $value; 6 7 // Constructor to initialize 'value' attribute 8 public function __construct($value) { 9 $this->value = $value; 10 } 11} 12 13// Define the derived class 'ChildClass' 14class ChildClass extends ParentClass { // ChildClass inherits from ParentClass 15 private $additional_value; 16 17 // Constructor to initialize 'value' and 'additional_value' 18 public function __construct($value, $additional_value) { 19 parent::__construct($value); 20 $this->additional_value = $additional_value; 21 } 22 23 // Method to display the values 24 public function display() { 25 echo "Value: " . $this->value . "\nAdditional Value: " . $this->additional_value . "\n"; 26 } 27} 28 29// Create an instance of 'ChildClass' and display its values 30$child_class = new ChildClass("value", "additional_value"); 31$child_class->display(); 32// Value: value 33// Additional Value: additional_value 34?>
These examples illustrate how calling the base class constructor in PHP promotes modular and efficient inheritance by enabling derived classes to expand upon existing functionalities cleanly.
We've successfully explored attribute and method inheritance in PHP with several practical examples. Understanding these concepts is vital for writing more efficient and readable PHP code. Remember, practice is key to mastering these concepts!
Are you ready for some practice exercises? These will help reinforce your understanding and prepare you for tackling more complex tasks. Enjoy the learning journey!