Inheritance, or the term Inheritance, is one of the key and fundamental concepts in programming. This concept allows us to create new classes based on a base class, inheriting the properties and behaviors of the base class, while also being able to have their own unique properties and behaviors. This unique feature means that the code can be reused and more maintainable.
In PHP, to create inheritance, we use the keyword extends. When we define a new class and want to inherit it from a base class, we simply need to mention the base class name after the extends keyword.
An example of a simple and practical use of inheritance could be a base class named Vehicle (means of transport) that includes properties like color and speed. Then we can define new classes like Car (car) and Motorcycle (motorcycle) that inherit from Vehicle and have their own specific properties such as the number of wheels for Motorcycle and the type of gear for Car.
Inheritance helps us to avoid code redundancy and maintain a more structured and manageable codebase. Similarly, we can make things simpler and utilize the properties of the base class in the derived classes as needed.
Example Code of Inheritance in PHP
<?php
class Vehicle {
public $color;
public $speed;
public function setProperties($color, $speed) {
$this->color = $color;
$this->speed = $speed;
}
public function displayInfo() {
echo "Color: " . $this->color . " | Speed: " . $this->speed . " km/h";
}
}
class Car extends Vehicle {
public $gearType;
public function setGearType($gearType) {
$this->gearType = $gearType;
}
public function displayCarInfo() {
$this->displayInfo();
echo " | Gear Type: " . $this->gearType;
}
}
$car = new Car();
$car->setProperties("Red", 150);
$car->setGearType("Automatic");
$car->displayCarInfo();
?>
Line-by-Line Code Explanation
class Vehicle {
This defines a base class named Vehicle.
public $color;
This defines a property for color for the class.
public $speed;
This defines a property for speed for the class.
public function setProperties($color, $speed) {
This defines a method to set the values of the color and speed properties.
$this->color = $color;
This assigns the value from the input parameter to the color property.
$this->speed = $speed;
This assigns the value from the input parameter to the speed property.
public function displayInfo() {
This defines a method to display the properties of the vehicle.
echo "Color: " . $this->color . " | Speed: " . $this->speed . " km/h";
This prints out the information of the vehicle.
class Car extends Vehicle {
This defines a class Car that inherits from Vehicle.
public $gearType;
This defines a property for gear type for the Car class.
public function setGearType($gearType) {
This defines a method to set the gear type property.
$this->gearType = $gearType;
This assigns the value from the input parameter to the gear type property.
public function displayCarInfo() {
This defines a method to display the details of the car.
$this->displayInfo();
This calls the displayInfo method from the base Vehicle class to show common vehicle information.
echo " | Gear Type: " . $this->gearType;
This adds the gear type information to the display.
$car = new Car();
This creates a new instance of the Car class.
$car->setProperties("Red", 150);
This sets the values of color and speed for the car.
$car->setGearType("Automatic");
This sets the gear type of the car to automatic.
$car->displayCarInfo();
This calls the method to display all car details including color, speed, and gear type.