PHP can't extend from interface?

ε祈祈猫儿з 提交于 2019-12-10 01:27:00

问题


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.


回答1:


You implement interfaces and extend classes:

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

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



回答2:


extends is for extending another class.

For interfaces, you need to use implements instead.

(An interface can extend another interface, though)




回答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.



来源:https://stackoverflow.com/questions/18829996/php-cant-extend-from-interface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!