How many types of inheritance in php

How many types of inheritance in php

Introduction to Inheritance in PHP

Inheritance is the way of extending the existing class functionality in the newly created class. We can also add some additional functionality to the newly created class apart from extending the base class functionalities. When we inherit one class, we say an inherited class is a child class (sub class) and from which we inherit is called the parent class. The parent class is also known as the base class. This is the way that enables the better management of the programming code and code reusability. The idea behind using the inheritance is all about better management of the code and the code reusability. In this topic, we are going to learn about Inheritance in PHP.

Types of Inheritance in PHP

PHP supports various types of inheritance like JAVA. The below table shows the list of inheritance types and the supporting status in PHP.

Inheritance Type Support in PHP
Single Inheritance YES
Multilevel Inheritance YES
Hierarchical Inheritance YES
Multiple Inheritance NO

1. Single Inheritance

PHP supports Single inheritance. Single inheritance is a concept in PHP in which one class can be inherited by a single class only. We need to have two classes in between this process. One is the base class (parent class), and the other a child class itself. Let’s understand the same with an example. It is popularly known as simple inheritance.  This type of inheritance in PHP language remains the same as JAVA, C++, etc.

How many types of inheritance in php

Code:

fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
class child extends MyAccess {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo $this->fist_name;
}
}
$obj1 = new child();
$obj1->setVal("Jai Shre");
$obj1->getVal();
?>

MyAccess is the parent, and the child is the name of the child class.

Output:

How many types of inheritance in php

2. Multilevel Inheritance

PHP supports Multilevel Inheritance. In this type of inheritance, we will have more than 2 classes. In this type of inheritance, a parent class will be inherited by a child class then that child class will be inherited by the child class. This type of inheritance in PHP language remains the same as C++ etc.

How many types of inheritance in php

Code:

fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
class child_1 extends ParentClass {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo "Extended By Parent Class -". $this->fist_name;
}
}
class child_2 extends child_1 {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo "Extended By child 1  - ".$this->fist_name;
}
}
$obj1 = new child_1();
$obj1->setVal("This is first inherited class");
$obj1->getVal();
echo "

"; $obj2 = new child_2(); $obj2->setVal("This is second inherited class"); $obj2->getVal(); ?>

Output:

How many types of inheritance in php

3. Hierarchical Inheritance

PHP supports Hierarchical inheritance. Hierarchical inheritance is the type of inheritance in which a program consists of a single parent and more than one child class. Let’s understand the same with this example. This type of inheritance in PHP language remains the same as JAVA, C++, etc.

How many types of inheritance in php

Code:

fist_name;
}
function set_fist_name($set_this){
$this->fist_name = $set_this;
}
}
class child_1 extends ParentClass {
function setVal($set_this){
$this->fist_name = $set_this;
}
function getVal(){
echo $this->fist_name;
}
}
class child_2 extends ParentClass {
function setVal($set_this){
$this->fist_name = $set_this." - ".$set_this;;
}
function getVal(){
echo $this->fist_name;
}
}
$obj1 = new child_1();
$obj1->setVal("This is first child class");
$obj1->getVal();
echo "

"; $obj2 = new child_2(); $obj2->setVal("This is second child class"); $obj2->getVal(); ?>

Output:

How many types of inheritance in php

We have one parent class named ParentClass and two child class, child_1 and child_2, respectively.  The given scenario of the inheritance is called Hierarchical Inheritance.

Importance of Inheritance in PHP

The importance of inheritance is many more as it comes up with huge advantages with it.

  • The code reusability is one of the most frequently used in the inheritance; the base class remains as it is in between the process. As we can see in the above example of all the inheritance, the code is being re-used from one class to another. We need not required to re-write the same thing again and again.
  • A base class can be used by a number of its derived classes in the class hierarchy. Yes, this is a type of inheritance in which we can go for extending in parent class with multiple inheritances.
  • Extensibility is one of the advantages of the inheritance in which we can extend the base class feature without making little or no changes to fulfill the business requirements. Suppose in the beginning we are just going with a parent class only with no child class. But in case of need, we can add the child class to fulfill our business needs later on.
  • Overriding is another advantage of this inheritance feature in which we can rewrite the definition of the base class function into the derived class to make changes as per the business requirements.
  • Less amount of code – meantime, we will have less code comparatively while moving ahead with the inheritance as compared to the traditional way of coding.
  • Inheritance also enabled the data hiding features as well. We can expose the only required part of the parent class to the child class using various PHP Access Modifiers.
  • Fully support MVC – we can go for MVC by using the concept of inheritance.

Conclusion

We should use the inheritance to fulfill our business as it comes up with some added advantages as compared to the normal code. We should take care of the data security while dealing with the inheritance. We can use the access modifier like private, protected to deal with the data hiding and data security. PHP does not support multiple inheritances.

This is a guide to Inheritance in PHP. Here we discuss the types of Inheritance in PHP, i.e. single, multilevel, hierarchical with the appropriate sample code. You may also look at the following article to learn more –

  1. Overloading in PHP
  2. Single Inheritance in Python
  3. Multilevel Inheritance in Java
  4. Multiple Inheritance in C++

How many types of inheritance are there?

There are different types of inheritance viz., Single inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance, and hierarchical inheritance. Single Inheritance: When a derived class inherits only from one base class, it is known as single inheritance.

What are the 3 types of inheritance?

Forms of Inheritance in Object Oriented Programming.
Single inheritance. This is a form of inheritance in which a class inherits only one parent class. ... .
Multiple Inheritance. An inheritance becomes multiple inheritances when a class inherits more than one parent class. ... .
Multi-level Inheritance..

Does PHP have multiple inheritance?

PHP doesn't support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it. Traits (Using Class along with Traits): The trait is a type of class which enables multiple inheritance.

What are the 6 types of inheritance?

Different Types of Inheritance.
Single inheritance..
Multi-level inheritance..
Multiple inheritance..
Multipath inheritance..
Hierarchical Inheritance..
Hybrid Inheritance..