PHP can't extend from interface?

15,827

Solution 1

You implement interfaces and extend classes:

<?php
interface people
{
    public function take($s);
}

class engineer implements people
{
    public function take($s){
        echo $s;
    }
}
?>

Solution 2

extends is for extending another class.

For interfaces, you need to use implements instead.

(An interface can extend another interface, though)

Solution 3

Depends on what you want, it could be:

  • class extends aClass
  • class implements anInterface
  • interface extends anInterface

You can extend only one class/interface and implement many interfaces. You can extend interface to another interface, e.g. interface DieselEngineInterface extends EngineInterface.

Also want to note a comment, now that you can have class and interface hierarchy, you need to know when to use them.

Share:
15,827
roast_soul
Author by

roast_soul

fire in the hole

Updated on June 24, 2022

Comments

  • roast_soul
    roast_soul about 2 years

    I write below in a single php file.

    <?php
    interface people
    {
        public function take($s);
    }
    
    class engineer extends people
    {
        public function take($s){
            echo $s;
        }
    }
    ?>
    

    The people is an interface, the engineer extends people. But when I run this code, the error:

    Fatal error: Class engineer cannot extend from interface people in E:\php5\Mywwwroot\b.php on line 12
    

    What's happened? My PHP version is 5.4.