articles

Home / DeveloperSection / Articles / Inheritance in PHP

Inheritance in PHP

Anonymous User 2729 18-Jun-2016

Inheritance is the one of the most important concept in object oriented programming. Inheritance is used to share properties and method between the related classes. When a class is derived from another class then the function of a parent class is inherited.

Creating a new class from existing class is called inheritance. When some class requires a same properties and methods of an existing class, then instead of recreating those members you can inherit the existing class, this is known as inheritance. So, inheritance is the process in which object of one class inherits the members of another class

One of the major advantages of inheritance is the ability to reduce code duplication. This occurs when a programmer write the same code many times. This problem is solved by inheritance in which we have a parent class having its own methods and properties, and a child class inherit these methods and properties. By using inheritance we can use parent class code in child class as much as possible.

With the help of inheritance we can get all the properties and functions of parent class into child class. Inheritance is done by using keyword extends after the name of the class and followed by parent class name. This is termed as reusability of code at higher level. Please note inheritance is introduced from PHP 5 it is not defined in previous versions of PHP.

Inheritance enables us to write the code only once in the parent class, and then
use the code in both the parent and the child classes.
The features of a child class are as follows:

·    Child class will automatically have all the variable of the parent class.

·    Child class will inherit all the member function of parent class.

·    Increase Reusability

As per the principle, a class can be derived from another class. A class which is derived is called child class or derived class or the sub class and the other class (from where it is derived) is called parent class or base class. Like in real life, child has some characteristics of parent same is with programming child class inherit the property of a parent class. Let’s understand with the help of an example:

Note: Parent class method and function is inherited only if it is public or protected access specifier.

<?php

 class parents
{
    public $fname;
    public function Greeting()
    {
        return "Hello, I'm parent class and my name is " . $this->name."</br>";
    }

}
class child extends parents //extends is a keyword using which child class inherit the parent class                                                //characteristics.
{
    public function Greeting()
    {
        return "Hello, I'm a child class and my name is " . $this->name;
    }
}
$parents = new parents();
$parents->name="Rahul";
echo $parents->Greeting();
$parents = new child();
$parents->name = "Varun";
echo $parents->Greeting();
?>
Display:
Hello, I'm parent class and my name is Rahul
Hello, I'm a child class and my name is Varun


Note
:
In the above example parents class has the method Greeting(), when child class inherited the parents class using extends keywords then all the properties of parents class is used by child class as in this case Greeting() method is reused by child class as a result code reusability.

Types of inheritance in PHP

Generally inheritance are of three types single, multiple, multi level inheritance but in case of PHP it only supports single inheritance, where only one class is derived from another class. As PHP does not support any type of inheritance except single inheritance, but we can simulate multiple inheritance using PHP interfaces. Here is an example of single inheritance:

<?php

 class sportscar
{
    public $fname;
    public function text()
    {
    }
}
class car extends sportscar //extends is a keyword using which child class inherit the parent                                                 // class characteristics
{
    public function text()
    {
        return "beep! I am a " . $this->name;
    }
}
$sportscar = new sportscar();
$sportscar = new car();
$sportscar->name = "Mercedes Benz";
echo $sportscar->text();
?>

 

Note: In the above example parents class has the method text(), when child class inherited the parents class using extend keywords then all the properties of parents class is used by child class as in this case text() method is reused by child class as a result code reusability.

The output of the above code is:   beep! I am a Mercedes Benz.


Updated 31-Mar-2019
I am a content writter !

Leave Comment

Comments

Liked By