Inheritance in PHP helps to reduce code replication. It is the way of reaching the current class functionality in the recently formed class. We are able to add some extra functionality to the newly formed class aside from reaching the base class functionalities. When we obtain one class, we declare an inherited class is a derived class (subclass), and from which we derive is called the parent class. The base class is also called the parent class. This is the process that allows the bigger control of the programming code and program reusability. The concept behind using the inheritance is all about more reliable control of the code and the code reusability. At this point, we are going to study Inheritance in PHP.
Types of Inheritance in PHP
There are 3 basic types of Inheritance that support in PHP. These are…
- Single Inheritance
- Hierarchical Inheritance
- Multilevel Inheritance
Single Inheritance
Single inheritance supports PHP. It is an idea in PHP in which a single class can be derived by one class unique. We require to have two classes in between this method. One is the parent class (base class) and the other a derived class (child class) itself. Let’s surmise the same with an example. It is commonly known as simple inheritance. This kind of inheritance in PHP language remains the same as other programming languages.
<?php class SOFT { var $var = "This is a first variable"; protected $fist_name; function returnVar() { echo $this->fist_name; } function set_fist_name($set_this){ $this->fist_name = $set_this; } } class child extends SOFT { function setVal($set_this){ $this->fist_name = $set_this; } function getVal(){ echo $this->fist_name; } } $obj1 = new child(); $obj1->setVal("Prophet Mohammad (P.B.U.H)"); $obj1->getVal(); ?>
Hierarchical Inheritance
Hierarchical inheritance supports PHP. It is the type of inheritance in which a code contains a single base and more than one derived class. Let’s surmise the same with this example. This type of inheritance in PHP language remains the same as other programming languages.
<?php class ParentSoft { var $var = "This is a first variable. </br>"; public $f_name; // simple class method function returnVar() { echo $this->f_name; } function set_f_name($set_this){ $this->f_name = $set_this; }} class child_0 extends ParentSoft { function setVal($set_this){ $this->f_name = $set_this; } function getValue(){ echo $this->f_name; }} class child_1 extends ParentSoft { function setVal($set_this){ $this->f_name = $set_this." - ".$set_this;; } function getValue(){ echo $this->f_name; }} $object1 = new child_0(); $object1->setVal("It is a first child class </br>"); $object1->getValue(); $object2 = new child_1(); $object2->setVal("It is a second child class"); $object2->getValue(); ?>
It is a second child class
It is a second child class
Multilevel Inheritance
Multilevel Inheritance supports PHP. Here, We need to have more than two classes. In this kind of inheritance, a base class will be inherited by a derived class then that derived class will be inherited by the derived class.
<?php class ParentSoft { var $var = "It is a first variable"; 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_0 extends ParentSoft { function setValue($set_this){ $this->fist_name = $set_this; } function getVal(){ echo "It has been extended By Parent Class -". $this->fist_name; } } class child_1 extends child_0 { function setValue($set_this){ $this->fist_name = $set_this; } function getVal(){ echo "It has been extended By child 1 - ".$this->fist_name; } } $object = new child_0(); $object->setValue("It is a first inherited class"); $object->getVal(); echo "<br/><br/>"; $obj2 = new child_1(); $obj2->setValue("it is a second inherited class"); $obj2->getVal(); ?>
It is a first inherited class.
It has been extended By child 1 – it is a second inherited class
Conclusion
We have to use the inheritance to meet our profession as it gets up with some added benefits as compared to the normal program. They should concern the care of data security while trading with the inheritance. It can use the access modifier (private, protected) to deal with data hiding and data security.