Inheritance in PHP?

In this blog, We are going to learn, what is inheritance in PHP?

What is Inheritance?

In PHP Object-Oriented Programming (OOPs), Inheritance is the phenomenon by which a child class can inherit all the properties and characteristics of the parent class.
In inheritance, you have a parent class with properties and methods, and a child class can use the code from the parent class.

To define a class inherits from another class, you use the extends keyword.

The syntax for Inheriting a Class in PHP

class Account extends BankAccount {

    // define member functions of

    // the derived class here.

}

derived_class_name(Account): It specifies the name of the derived or the child class. The child class, also known as a subclass, inherits its main features or characteristics from its parent class. There can be one or over one derived class that inherits from the same base class depending on what type of inheritance is performed.

base_class_name(BankAccount): It specifies the name of the base or the parent class from which the child class is inheriting its properties. A base class is also known as a parent class because all the classes that are termed derived classes or child classes inherit their properties from the base class.

Inheritance has three types, which are given below.

  • Single inheritance
  • Multiple inheritance
  • Multi-level inheritance

Note: But PHP supports only single inheritance, where only one class can be derived from the single parent class. We can also use multiple inheritances by using interfaces.

I hope that you found this Blog helpful. If you’ve any queries, please leave comments in the comment section.

Thanks for following and reading this blog.


Inheritance in PHP?expoundsofttechsolution

ghb