What is multilevel inheritance in php?

Multiple Inheritance is the property of the Object Oriented Programming languages in which child class or sub class can inherit the properties of the multiple parent classes or super classes.


PHP doesn’t support multiple inheritance but by using Interfaces in PHP or using Traits in PHP instead of classes, we can implement it.

Traits [Using Class along with Traits]: The trait is a type of class which enables multiple inheritance. Classes, case classes, objects, and traits can all extend no more than one class but can extend multiple traits at the same time.
Syntax:

class child_class_name extends parent_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}

Example:

Output:

Hello Geeks
GeeksforGeeks

In the above program “traits” has been used along with parent class. There is “class” named “Geeks” which contains function sayhello[] and a “trait” named “forGeeks” which contains function geeksforgeeks[] and there is child class named “Sample” and we are creating the object of this class named “test” and using it we are invoking all the functions of a class and a trait.

Traits [Using Multiple Traits]: Multiple Traits can be inserted into a class by listing them in the use statement, separated by commas.
Syntax:

class child_class_name {
    use trait_name;
    ...
    ...
    child_class functions
}

Example:

Output:

Hello Geeks
GeeksforGeeks

In the above program “traits” has been used. There are two traits named “Geeks” which contains function sayhello[] and “forGeeks” which contains function geeksforgeeks[] respectively and there is a child class “Sample” and we are creating the object of this class named “test” and using it we are invoking all the functions of traits.

Interface [Using Class along with Interface]:
Syntax:

class child_class_name extends parent_class_name implements interface_name1, ...

Example:

Output:

I am in class A
I am in interface
I am in inherited class

In the above program Interface “B” has been used along with the class “A” to implement multiple inheritance. The important point to remember is, it can’t define the function inside interface, it should be defined inside the child class “Multiple”. We are invoking all the functions using the child class [Multiple] object named “geeks”.

Interface [Using Multiple Interface]:

Syntax:

class child_class_name implements interface_name1, interface_name2, ...

Example:

Output:

I am in interface C
I am in interface B
I am in inherited class

In the above program multiple interfaces has been used to implement multiple inheritance. In above example there are two interfaces named “B” and “C” those are playing the role of the base classes and there is child class named “Multiple” and we are invoking all the functions using its object named “geeks”.

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


What is multilevel inheritance?

In the Multilevel inheritance, a derived class will inherit a base class and as well as the derived class also act as the base class to other class. For example, three classes called A, B, and C, as shown in the below image, where class C is derived from class B and class B, is derived from class A.

What is multiple inheritance explain with example?

Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor.

What is types of inheritance in PHP?

Inheritance has three types, single, multiple and multilevel Inheritance. PHP supports only single inheritance, where only one class can be derived from single parent class. We can simulate multiple inheritance by using interfaces.

Why multiple inheritance is not used in PHP?

As you can see from the code, on calling the method greet[] using object ClassC , it's impossible for the compiler to decide whether it has to call ClassA's greet[] or ClassB's greet[] method. So, this is to avoid such complications, PHP does not support multiple inheritance.

Chủ Đề