17

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.

6
  • 12
    Classes implement interfaces, not extend them. Commented Sep 16, 2013 at 14:03
  • PHP uses the methodology as Java to prevent the diamond problem - See en.wikipedia.org/wiki/Multiple_inheritance - hence the implements keyword Commented Sep 16, 2013 at 14:10
  • Thank you all of you. Foolish me. Commented Sep 16, 2013 at 14:13
  • Just as an aside here, whilst your issue is technically fixed, this example might be better suited to class inheritance as opposed to interface implementation. That is, an engineer is a type of person, and will take on most of the methods and functionality of a person, plus additional, engineer specific, methods. Therefore, you're not implementing an interface, but are instead extending the 'person' class. I would certainly not go as far as to make 'person' an abstract class or anything, though, as it seems reasonable that you may want to use the base class in isolation. Commented Feb 26, 2014 at 12:32
  • @roast_soul Did my response answer your question? Commented Sep 22, 2014 at 20:14

3 Answers 3

44

You implement interfaces and extend classes:

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

class engineer implements people
{
    public function take($s){
        echo $s;
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Man, some days I feel dumb. For some reason, that PHP error just doesn't make me think "oh, I wrote extends not implements".
@bishop No, you are not dumb, the error message is dumb. All they would have had to do is spend 2 seconds thinking about what the clearest error message would be, and bam, problem solved for all PHP programmers around the world. But instead they chose an error message that makes it seem like there is something about that particular interface and that particular class trying to extend it that will not work, rather than the basic idea that any class can never extend any interface.
18

extends is for extending another class.

For interfaces, you need to use implements instead.

(An interface can extend another interface, though)

2 Comments

But a method signature m1(AInterface $obj) in class A cant be overriden by m1(BInterface $obj) even
"Interfaces can be extended like classes using the extends operator." php.net - interfaces
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.