Javatpoint Logo
Javatpoint Logo

Multiple Inheritance in PHP

What is Multiple Inheritance?

Multiple inheritances are one of the four pillars of OOP object-oriented programming, consisting of a child class or a subclass inheriting the traits from multiple parent classes or superclasses.

These classes can reuse the code derived from their parent class after enabling a relationship between a child and parent class using the basic logic of hierarchy. Using multiple inheritances increases compilation speed, reducing development time and providing a higher accuracy level.

Object-Oriented Programming

  1. Object-oriented programming, also known as OOP, is a structured programming method used to organize code blocks around data and objects instead of functions and logic.
  2. Objects: Objects are data fields with a specific characteristic and set of functionality.
  3. Object-oriented programming mainly focuses on the data objects created by the developer that they want to manipulate to perform a certain task rather than focusing on the logic required to manipulate the object.
  4. This type of programming is highly effective for programs with a very high complexity of data that must be updated regularly or maintained thoroughly.
  5. Some other advantages of object-oriented programming include code reusability, scalability and efficiency.
  6. The main principle of OOP consists of
    • Data Encapsulation
    • Data Abstraction
    • Inheritance
    • Polymorphism

Multiple Inheritance in PHP

PHP does not have multiple inheritance properties, but we can still use multiple inheritances in PHP with the help of using interfaces provided in PHP or traits and classes.

Traits and interfaces are special characteristics of single inherited programming languages such as PHP, where multiple inheritances are impossible. A trait helps the developer reduce the limitations of PHP's single inheritance property and helps reuse code freely in different classes that present different levels in a hierarchy. Traits are almost similar to a class, but they consistently group functionality, unlike classes. It is not possible to initiate a trait on its own that's why it is necessary to use it in class. We can say that trait is a type of class that helps the developer with multiple inheritances by using multiple traits simultaneously.

Traits (Using Class along with Traits)

Syntax:

Example:

Output:

Multiple Inheritance in PHP

In the above program, we have a parent class that contains a capacity parent function(), and we have a quality called for the parent class, which holds the capacity named tract function(). Eventually, we have a youngster class with the name child class with work child function() and acquired the quality for parent class with use condition. Also, to get to the classes and qualities, we have made an article with the name obj and called every one of the capacities utilizing a similar item.

Traits (Using Multiple Traits)

Instead of using traits with the class, there is another method to use multiple inheritances by using multiple traits

Syntax:

Example:

Output:

Multiple Inheritance in PHP

In the above program, we have declared two traits first trait with the name firstTrait with function first trait() and the second trait with the name secondTrait with function second trait(). Finally, we have a kid class with the name child class with work child function() and acquired both qualities using proviso. Also, to get to the classes and characteristics, we have made an article with the name obj and called every one of the capacities utilizing a similar item.

Interface (Using Class along with Interface)

Syntax:

Example:

Output:

Multiple Inheritance in PHP

We have a parent class in the above program that contains a capacity parent function(). We have a point of interaction called the first interface, which holds the capacity named interface function(). We can't characterize the pronounced capacity inside our made connection point, and we need to characterize the proclaimed capacity inside the kid class. Eventually, we have a kid class with the name child class with work child function() and acquired the capacity from the point of interaction utilizing the expands and execute property of PHP. Additionally, we have characterized our interface function() in the youngster. Also, to get to the classes and point of interaction, we have made object obj and summoned every one of the capacities utilizing a similar article.

Interface (Using Multiple Interface)

Syntax:

Example:

Output:

Multiple Inheritance in PHP

In the above program, we have pronounced two interaction points, first with the name first interface with work interface one() and second with the name second interface with work outerfacetwo(). Ultimately, we have a kid class with the name child class with work child function() and acquired both the points of interaction utilizing the carries out provision. We have given the meaning of capacities pronounced in interfaces inside the capacity. Furthermore, to get to the classes and connection points, we have made an item with the name obj and called every one of the capacities utilizing a similar article. We have just pronounced the connection point capacities and will characterize a similar capacity in the youngster class.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA