PHP OOP class inheritance

A class that has inheritance in PHP OOP is one that obtains or gets all the properties and methods from its parent class.

This is useful when you need to set a base class that is applicable and relevant throughout the code however you want to avoid duplicating code.

The child class can use code from its parent however it doesn’t have to.

Easy examples are a parent class of Cool Drinks and then children like Water, Apple juice, Beer, Sports drink, Tea etc. The parent class can combine/get/output the properties whilst the child sets taste, availability, price etc (pre-defined or not).

Code example

Here is a PHP OOP class inheritance example using vehicles, The parent class Vehicle is a setter and getter for properties that apply to all vehicles such as cars, trucks, motorbikes and boats.

All of these vehicles have a model name, year made and color.

A child class is car, here there is predefined vehicle type of ‘car’ and wheels = 4. However, depending on the car I can change safety rating and airbag count.

<?php

//Parent class
class vehicle
{
    private $model;
    private $color;
    private $year_made;

    public function setModel($model)
    {
        $this->model = $model;
    }

    public function getModel()
    {
        return $this->model;
    }

    public function setColor($color)
    {
        $this->color = $color;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setYearMade($year_made)
    {
        $this->year_made = $year_made;
    }

    public function getYearMade()
    {
        return $this->year_made;
    }


    public function output()
    {
        return "Vehicle type:" . $this->getType() . " wheels:" . $this->getWheels() . " model:" . $this->getModel() . "
           year:" . $this->getYearMade() . " color:" . $this->getColor() . " safety:" . $this->getSafetyRating() . " airbags:" . $this->getAirBags() . "";
    }
}

//Child class inherits the code from the parent class above
class car extends vehicle
{
    private $type = 'car';
    private $wheels = 4;
    private $safety_rating;
    private $air_bags;

    public function setSafetyRating($sr)
    {
        $this->safety_rating = $sr;
    }

    public function setAirBags($air_bags)
    {
        $this->air_bags = $air_bags;
    }

    public function getType()
    {
        return $this->type;
    }
}

//Create new instance of a car which is a vehicle (parent class)
$sedan = new car();
//Set model as a 'speedy'
$sedan->setModel('Speedy');
//Set year made
$sedan->setYearMade('2015');
//Set color
$sedan->setColor('Olive');
//Set safety rating
$sedan->setSafetyRating('4/5');
//Set airbags
$sedan->setAirBags(6);
//Output
echo $sedan->output();
//Vehicle type:car wheels:4 model:Speedy year:2015 color:Olive safety:4/5 airbags:6

 

Adding another child class which is motorbike:

//Another Child class
class motorbike extends vehicle
{
    private $type = 'Motorbike';
    private $wheels = 2;
    private $safety_rating = '0/5';
    private $air_bags = 0;

    public function getSafetyRating()
    {
        return $this->safety_rating;
    }

    public function getWheels()
    {
        return $this->wheels;
    }

    public function getAirBags()
    {
        return $this->air_bags;
    }

    public function getType()
    {
        return $this->type;
    }

}
//Create new instance of a car which is a vehicle (parent class)
$bike = new motorbike();
//Set model as a 'speedy'
$bike->setModel('Cruiser');
//Set year made
$bike->setYearMade('2011');
//Set color
$bike->setColor('Silver');
//Output
echo $bike->output();
//Vehicle type:Motorbike wheels:2 model:Cruiser year:2011 color:Silver safety:0/5 airbags:0