Inheritance in PHP Object-Oriented Programming

Introduction

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!

Defining Inheritance

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:

PHP
<?php

// Define the base class 'Vehicle'
class Vehicle {
    protected $color;
    protected $brand;

    // Constructor to initialize 'color' and 'brand' attributes
    public function __construct($color, $brand) {
        $this->color = $color;
        $this->brand = $brand;
    }
}

// Define the derived class 'Car'
class Car extends Vehicle { // Car inherits from Vehicle
    private $doors;

    // Constructor to initialize 'color', 'brand', and 'doors'
    public function __construct($color, $brand, $doors) {
        parent::__construct($color, $brand); // Call the parent class constructor
        $this->doors = $doors;
    }
}

?>

We will focus primarily on single inheritance in PHP, where one base class is extended by one derived class.

Attribute Inheritance

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:

PHP
<?php

// Define the base class 'Artist'
class Artist {
    protected $name;

    // Constructor to initialize 'name' attribute
    public function __construct($name) {
        $this->name = $name;
    }
}

// Define the derived class 'Musician'
class Musician extends Artist { // Musician inherits from Artist
    private $instrument;

    // Constructor to initialize 'name' and 'instrument' attributes
    public function __construct($name, $instrument) {
        parent::__construct($name); // Call the parent constructor
        $this->instrument = $instrument;
    }

    // Method to display the musician's details
    public function display() {
        echo "Name: " . $this->name . "\nInstrument: " . $this->instrument . "\n";
    }
}

// Create an instance of 'Musician' and display its details
$john = new Musician("John Lennon", "Guitar");
$john->display();
// Name: John Lennon
// Instrument: Guitar
?>

The Musician class inherits the name attribute from the Artist class and also possesses its unique attribute, instrument.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal