PHP can not extend from the interface?

I write below in one php file.

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

People is an interface; an engineer expands people. But when I run this code, the error is:

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

What happened? My PHP version is 5.4.

+6
source share
4 answers

You implement interfaces and extend classes:

 <?php interface people { public function take($s); } class engineer implements people { public function take($s){ echo $s; } } ?> 
+29
source

extends intended to extend another class.

For interfaces you need to use implements .

(An interface can extend another interface, though)

+18
source

Depending on what you want, this might be:

  • class extends aClass
  • class implements anInterface
  • The interface extends the interface.

You can extend only one class / interface and implement many interfaces. You can extend the interface to another interface, for example. The DieselEngineInterface interface extends the EngineInterface interface.

I also want to note a comment, now that you can have a hierarchy of classes and interfaces, you need to know when to use them.

+1
source

I use: interface xyz{…} . Then class abc implements xyz we get:

The xyz type cannot be the abc superinterface; supersurface must be an interface

OK! so extend abc from xyz I get:

Class abc cannot extend from xyz interface

works in eclipse "oxygen" php 7 GOOD WORK!

-1
source

Source: https://habr.com/ru/post/953924/


All Articles