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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.