Inheritance in PHP

Inheritance is an object oriented programming feature. Just as a child inherits the inheritance of his parents, in the same way a class can inherit the properties and functions of a different class.

The class which is inherited is called parent or base class. Inherit-to-class child or sub-class is called. Instead of using properties and functions of the child class parent class, it can declare properties and functions of itself as well.

There are many benefits to using inheritance. The most important benefit of Inheritance is that you can use any property or function declared in the base class as a child class. It saves you time and also reduces the code in the program, which reduces the complexity of the program.

The child class does not need to declare or define them back to access the members of the base class. Just as a normal class accesses its properties and functions, a child class can access base class class.

Any child class can access only public and protected members of a parent class.

Types of Inheritance

The different types of inheritance are being described below.

Single Inheritance – In this kind of inheritance, one class inherits another class. It is also called a normal inheritance.

Multilevel Inheritance – In this kind of inheritance, the inheriting class is also inherited. For example, if there is a class B that inherits Class A, and there is a class C who inherits class B, then inheritance is called a multilevel inheritance.

Multiple Inheritance – In this kind of inheritance, one class inherits more than one class. This type of inheritance is not allowed in PHP.

Multiple inheritance in PHP can be implemented by Interfaces. A class can implement multiple interfaces.

Syntax of PHP Inheritance

Normal syntax of inheriting any class in PHP is being given below.

<?php 
class parentClass
{
// Properties & functions of parent class
}
class childClass extends parentClass
{
// Can use public & propertected properties & functions of parent class
// Child class own properties & functions
}
?>

In PHP, when a class inherits another class, it does so by the keyword extends. As you can see in the syntax above, childClass extends is inheriting parentClass by keyword.

Example

A simple example of implementing single inheritance in PHP is being given below.

<?php
// Parent class
class myClass
{
public $firstname = "Chaurasiya";
}
// Child class
class myOtherClass extends myClass
{
public function displayName()
{
// Accessing parent class property
echo $firstname;
}
}
?>
Cialis (Tadalafil) är den främsta konkurrenten till Viagra (Sildenafil) på marknaden för erektil dysfunktion. köpa Cialis i Sverige föredras av många på grund av sin längre varaktighet och anses vara det mest kostnadseffektiva varumärkesbaserade ED-läkemedlet som finns tillgängligt i Sverige. Cialis finns i två varianter: Cialis och Cialis Daily, och fyra olika doseringar: 2,5 mg, 5 mg, 10 mg och 20 mg, erbjuder Cialis också en rad olika alternativ för att passa patientens behov.