×
☰ See All Chapters

PHP Inheritance

A class should be closed for modification but should be open for extension. We should not modify any class; we can add additional features to an existing class without modifying it. Inheritance is one concept to achieve this. Inheritance is a concept of inheriting the properties of one class (super class) into another class (sub class). It is a process of reusing existing class functionality in new class. The existing class is called super class/parent class/base class and the new class is called subclass/child class/derived class. All the members of super class will be inherited into subclass including __construct and __destruct functions (except private members) and can be accessed directly in subclass. Super classes are generalized classes, subclasses are specialized classes.

When you don’t define __construct function in subclass, then __construct function will be inherited to subclass. In this case when object is created for subclass you should pass necessary arguments for __construct function inherited from superclass.

Syntax to create subclass

class SubclassName extends SuperclassName  {

    //Members of subclass

}

Different types of Inheritance

1) Simple Inheritance

2) Multilevel Inheritance.

3) Hierarchical Inheritance.

1) Simple Inheritance

In simple inheritance there will be exactly one super class and one subclass i.e. subclass will get the functionality from exactly only one super class. It is supported by class data type.

php-inheritance-0
 

<?php

class Box {

   public $width;

   public $height;

   public $depth;

   

   function __construct($width, $height, $depth) {

       $this->width = $width;

       $this->height = $height;

       $this->depth = $depth;

   }

   

   public function volume () {

       return  $this->width *  $this->height *  $this->depth;

   }

   

}

 

class BoxWeight extends Box {

    public $mass;

   

    function __construct($width, $height, $depth, $mass) {

        $this->width = $width;

        $this->height = $height;

        $this->depth = $depth;

        $this->mass = $mass;

    }

   

    public function density () {

        return  $this->volume () * $this->mass;

    }

}

 

$boxWeight = new BoxWeight(10, 20, 30, 40);

 

?>  


2) Multilevel Inheritance

In multilevel inheritance one super class can have many sub classes. One subclass can have many indirect super classes i.e. one is direct super class all remaining are indirect super classes.

  php-inheritance-1
 

<?php

 

class Aves {

    public function nature() {

        echo "Generally, Aves fly";

    }

}

 

class Bird extends Aves {

    public function eat() {

        System.out.println("Eats to live");

    }

}

 

class Parrot extends Bird {

    public function food() {

        echo "Parrot eats seeds and fruits";

    }

}

 

?>  

3) Hierarchical Inheritance

In this one super class can have many direct sub classes. It is supported by class data type.

php-inheritance-2
 

<?php

 

class Aves {

    public function nature() {

        echo "Generally, Aves fly";

    }

}

 

class Parrot extends Aves {

    public function eat() {

        echo "Parrot eats fruits and seeds";

    }

}

 

class Vulture extends Aves {

    public function vision() {

        echo "Vulture can see from high altitudes";

    }

}

 

?>  

Inherited __construct function

When you don’t define __construct function in subclass, then __construct function will be inherited to subclass from superclass. In this case when object is created for subclass you should pass necessary arguments for __construct function inherited from superclass.

<?php

class Box {

    public $width;

    public $height;

    public $depth;

   

    function __construct($width, $height, $depth) {

        $this->width = $width;

        $this->height = $height;

        $this->depth = $depth;

    }

   

    public function volume () {

        return  $this->width *  $this->height *  $this->depth;

    }

   

}

 

class BoxWeight extends Box {

    public $mass;

    public function density () {

        return  $this->volume () * $this->mass;

    }

}

 

$boxWeight = new BoxWeight(10, 20, 30);

$boxWeight->mass = 40;

 

?>

Overriding Inherited Methods

Inherited methods can be overridden by redefining the methods with the same name in sub class. Overridden methods should have same parameters list.

<?php

class Colour {

    public function printColourname($colour) {

        echo "Colour : ". $colour;

    }

}

 

class Red extends Colour {

    public function printColourname($colour) {

        echo "Colour : ". $colour;

    }

}

 

$red = new Red();

echo $red->printColourname("Red"). "</br>"; // Red

 

$colour = new Colour();

echo $colour->printColourname("Orange"); // Orange

?>

 

The final Keyword

final is keyword can used with classes and functions. When a class is made final, class cannot be extended. When a function is made final, it cannot be overridden.

<?php

 

final class Fruit {

    // some code

}

 

// will result in error

class Strawberry extends Fruit {

    // some code

}

 

class Colour {

    final public function printColourname($colour) {

        echo "Colour : ". $colour;

    }

}

 

class Red extends Colour {

    public function printColourname($colour) { //Fatal Error, cannot override final funtion

        echo "Colour : ". $colour;

    }

}

 

?>

 


All Chapters
Author