Home > Article > Backend Development > Inheritance in PHP
Inheritance is a way of extending the existing class functionality in the newly created class. We can also add 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), which is called the parent class from which we inherit it. The parent class is also known as the base class. This is the way that enables better management of the programming code and code reusability. The idea behind using 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.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
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 |
PHP supports Single inheritance. Single inheritance is a concept in PHP in which only one class can be inherited by a single class. We need to have two classes in between this process. One is the base (parent) class, and the other is the child class. 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.
Code:
<?php class MyAccess { var $var = "This is first var"; protected $fist_name; // simple class method function returnVar() { echo $this->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’s class.
Output:
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 in C++ etc.
Code:
<?php class ParentClass { var $var = "This is first var"; public $fist_name; // simple class method function returnVar() { echo $this->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 "<br/><br/>"; $obj2 = new child_2(); $obj2->setVal("This is second inherited class"); $obj2->getVal(); ?>
Output:
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.
Code:
<?php class ParentClass { var $var = "This is first var"; public $fist_name; // simple class method function returnVar() { echo $this->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 "<br/><br/>"; $obj2 = new child_2(); $obj2->setVal("This is second child class"); $obj2->getVal(); ?>
Output:
We have one parent class named ParentClass and two child classes, child_1, and child_2, respectively. The given scenario of inheritance is called Hierarchical Inheritance.
The importance of inheritance is many more as it has huge advantages.
We should use inheritance to fulfill our business as it has some advantages compared to the normal code. We should take care of data security while dealing with inheritance. We can use access modifiers like private and protected to deal with data hiding and data security. PHP does not support multiple inheritances.
The above is the detailed content of Inheritance in PHP. For more information, please follow other related articles on the PHP Chinese website!