Inheritance in PHP

PHPz
Release: 2024-08-29 12:59:34
Original
1234 people have browsed it

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 Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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 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.

Inheritance in PHP

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();
?>
Copy after login

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

Output:

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 in C++ etc.

Inheritance in PHP

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();
?>
Copy after login

Output:

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.

Inheritance in PHP

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();
?>
Copy after login

Output:

Inheritance in PHP

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.

Importance of Inheritance in PHP

The importance of inheritance is many more as it has huge advantages.

  • Code reusability is one of the most frequently used in 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 be required to rewrite 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 extend in parent class with multiple inheritances.
  • Extensibility is one of the advantages of inheritance in which we can extend the base class feature without making little or no changes to fulfill the business requirements. Suppose we just go with a parent class with no child class. But in case of need, we can add the child class to fulfill our business needs later.
  • 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 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!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!