php
  1. php-inheritance

PHP Inheritance

Inheritance is a concept in object-oriented programming that allows a class to inherit properties and functions from another class. In PHP, inheritance is implemented using the extends keyword.

Syntax

The syntax for creating a subclass that extends from a parent class in PHP is as follows:

class ChildClass extends ParentClass {
    // code for child class
}

Example

Let's consider an example where we have a Vehicle class and a Car class that extends from the Vehicle class with some additional functionality:

class Vehicle {
    public $brand;
    public $vehicleType;
    
    public function __construct($brand, $vehicleType) {
        $this->brand = $brand;
        $this->vehicleType = $vehicleType;
    }
    
    public function about() {
        echo "This is a $this->vehicleType made by $this->brand.";
    }
}

class Car extends Vehicle {
    public $model;
    
    public function __construct($brand, $model) {
        parent::__construct($brand, "car");
        $this->model = $model;
    }
    
    public function about() {
        parent::about();
        echo " The model is $this->model.";
    }
}

// create a car object
$car = new Car("BMW", "M4");
$car->about(); // Output: This is a car made by BMW. The model is M4.

In the above example, the Car class extends from the Vehicle class and inherits the brand property and about() function. The Car class also has its own model property and overrides the about() function to add the model to the output.

Explanation

Inheritance allows for the creation of a new class that is a modified version of an existing class. The new class, called the subclass or child class, inherits all the properties and methods of the existing class, called the superclass or parent class. The child class can then add or modify its own variables and methods.

When extending a class in PHP, the child class is created using the extends keyword followed by the name of the parent class. The child class can call the constructor of the parent class using the parent::__construct() function. This allows the child class to use the properties and methods of the parent class.

Use

Inheritance is useful in object-oriented programming when we want to create a subclass that is similar to an existing class but with some additional functionality. It avoids the need to copy-paste code and allows for code reuse. Inheritance also allows us to create a hierarchy of classes that can be organized in a logical way.

Important Points

  • A subclass can only extend from one parent class.
  • Private properties and methods of the parent class are not inherited by the child class.
  • A child class can override the properties and methods of the parent class with its own.
  • A child class can call the constructor of the parent class using the parent::__construct() function.
  • A child class can use the properties and methods of the parent class using the parent:: keyword.

Summary

Inheritance is a concept in object-oriented programming that allows for code reuse by creating a subclass that extends from an existing class. In PHP, inheritance is implemented using the extends keyword. The child class inherits the properties and methods of the parent class and can add or modify its own variables and functions. Inheritance is useful when creating a hierarchy of classes that have similar functionality.

Published on: