Google News
logo
PHP program to implement multiple interfaces in the same class
In the following example of PHP program that demonstrates how to implement multiple interfaces in the same class :
Program :
<?php

interface Greeting {
    public function sayHello();
}

interface Departure {
    public function sayGoodbye();
}

class MyClass implements Greeting, Departure {
    public function sayHello() {
        echo "Hello, world!\n";
    }

    public function sayGoodbye() {
        echo "Goodbye, world!\n";
    }
}

$obj = new MyClass();
$obj->sayHello();
$obj->sayGoodbye();

?>
Output :
Hello, world!
Goodbye, world!
In this example, we have two interfaces `Greeting` and `Departure`, each with one method. We then define a class `MyClass` that implements both of these interfaces and defines the methods required by each.

Finally, we create an object of `MyClass` and call its methods to output "Hello, world!" and "Goodbye, world!".