Is multiple inheritance supported in PHP?

No. PHP does not support multiple inheritance for classes. A class can extend only one parent class, so class C extends A, B gives an error.

But there are two ways to get similar result:

  • Interfaces: a class can implement many interfaces.
  • Traits: a class can use many traits to reuse code. See What are traits in PHP?.
<?php
interface Logger {
    public function log($msg);
}

interface Mailer {
    public function send($to);
}

class Notifier implements Logger, Mailer {
    public function log($msg) {
        echo "Log: " . $msg . "<br>";
    }

    public function send($to) {
        echo "Mail sent to " . $to;
    }
}

$n = new Notifier();
$n->log("Notifier started");
$n->send("robin@example.com");
?>

Output:

Tutorials Class - Output Window

Log: Notifier started
Mail sent to robin@example.com

Multilevel inheritance (C extends B, and B extends A) is supported, only multiple parents at same time is not.


Learn more about the similar topics:
Tutorials
PHP OOP Introduction
PHP Class
PHP Objects
Exercises & Assignments
No Content Found.
Interview Questions & Answers
How to create a class and object in PHP?
What is the difference between public, private and protected in PHP?
Is multiple inheritance supported in PHP?
What is the meaning of final class and final method in PHP?
What is the difference between abstract class and interface in PHP?
What are traits in PHP?
What is a constructor in PHP?