Search code examples
phpoopinterface

Why do all functions in an interface have to be public?


As stated in the title: why does each function you add to an interface has to be public?

All methods declared in an interface must be public; this is the nature of an interface

http://php.net/interface

What is the nature of an interface as stated in the citation above?

How about having a class implement an interface, and another class extending that class. Why is it not possible to define the methods necessary in the classes that extend the main class?

Please note: I do know how to use interfaces, but I'm just wondering why these things are not possible to predefine.


Solution

  • On a more general (Non-PHP specific) level, interfaces provide a listing of methods that the class promises to make available for use by other objects.

    A private method in an interface doesn't get you anything because only the implementing class would be able to use it. Therefore anything marked private may as well not be listed in the interface.