-3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Daniel Gelling
  • 892
  • 1
  • 9
  • 22
  • Nature of an interface is that all methods declared in an interface must be public – Marcin Orlowski Nov 07 '14 at 13:24
  • The functions has to be public otherwise you can get errors! But a good thing :D you don't have to write public because the are as default public! – Rizier123 Nov 07 '14 at 13:27
  • 2
    Classes that implement the interface would not be able to use the signatures if they were private or protected. – Christopher Lamm Nov 07 '14 at 13:28
  • Could I ask why this question is voted down? Since this is a question which is quite a good one for programmers new to interfaces... – Daniel Gelling Nov 07 '14 at 13:54
  • @DanielGelling: one possible reason is, excistance of duplicate questions (see: http://stackoverflow.com/questions/17576/non-public-members-for-c-sharp-interfaces or search for public+interface) – Allmighty Nov 07 '14 at 13:59

6 Answers6

6

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.

MichaelPlante
  • 452
  • 1
  • 4
  • 14
  • And 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? – Daniel Gelling Nov 07 '14 at 13:33
  • @DanielGelling Because: How can you extend from a interface where the function it's not defined(behavior)! What do you want to inherit?!? – Rizier123 Nov 07 '14 at 13:37
  • 1
    My understanding is that since they inherit from the implementing class, they're guaranteed to have the implementations of all the interface methods. They can override them if you want, but they don't have to. – MichaelPlante Nov 07 '14 at 13:40
  • If you want to force a specific way of implementation, you can use Abstract Classes (http://php.net/manual/en/language.oop5.abstract.php) for that. – jgroenen Nov 07 '14 at 15:21
1

An interface allows you to define methods without actually implementing them, for example:

public function setVariable($name, $var);

Notice the ; at the end of the function, whereas you would usually put { with the rest of your code.

When a class implements an interface, it is expected to implement all the defined methods, for example:

public function setVariable($name, $var) {
    //do more stuff here
}

So making one private would be pointless as the implementing class would not be able to access it.

Daniel Stanley
  • 1,050
  • 6
  • 15
  • Don't know how down voted. I think it is a good answer! You show why it is not possible to make private functions in a interface! +1 – Rizier123 Nov 07 '14 at 13:34
  • @Rizier123 thank-you! Yes it would be nice for the down vote to be explained. But there we go... – Daniel Stanley Nov 07 '14 at 13:36
  • 1
    I haven't downvoted (yet) but your answer, to me, doesn't cover the real reasons for having public functions/methods in an interface. A return question following your answer would be: "Why can't we have protected functions/methods in an interface"? – Allmighty Nov 07 '14 at 13:55
1

The idea behind the concept of interfaces is to separate the external interface of a class from the internal implementation of it. It is used to ensure to other classes using the class implementing the interface that the functions they expect it to have are actually there. If you want to force a specific way of implementation, you can use Abstract Classes (http://php.net/manual/en/language.oop5.abstract.php) for that.

jgroenen
  • 1,332
  • 1
  • 8
  • 13
0

The nature of an interface is to expose a set of behavior that's why it's implicitly public.. so if it is private you don't expose the behavior..

Gael
  • 167
  • 6
0

you can drive all types of car because it has same interfaces but you can't drive plane that diffrent than car interface

-2

Functions in a Interface has to be public otherwise you can get erros!

An example is this:

interface A
{
    private method1();
}

You will get a error like this:

Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE

So you have to write public functions in a interface!

Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • 1
    I would assume that it's because the question is asking more for an explanation of why php doesn't allow it. This doesn't really explain why it can't be done and instead says something along the lines of "you can't do it because you can't do it." – MichaelPlante Nov 07 '14 at 13:45
  • 2
    You aren't answering the question which asks what the reasoning is behind the fact that methods described in an interface are public. The answer you provide is a consequence of that fact. – Allmighty Nov 07 '14 at 13:45