Is it possible to have an interface with private / protected methods?

Is it possible in PHP 5 to have an interface with private / protected methods?

Now I have:

interface iService { private method1(); } 

This causes an error:

Parse error: syntax error, unexpected T_STRING, waiting for T_VARIABLE

I just want to get confirmation that in this case the interface can only contain public methods.

+50
php interface
Dec 09 '09 at 20:09
source share
5 answers

The PHP man page for interfaces explicitly states:

All methods declared in the interface must be publicly available, this is the nature of the interface.

I think this explains the error you are getting; -)

+85
Dec 09 '09 at 20:15
source share

Interfaces are used to describe the public methods of a class that implements this interface. You will never have a private method in the interface. It is assumed that any methods in the interface are used and should not be changed.

Interfaces is a reference to PHP, but this is standard for OO programming.

+20
Dec 09 '09 at 20:13
source share

In general, an interface can only have public elements, since the only function of the interface must be inherited.

From the tutorial PHPfreaks.com:

PHP5 has interfaces. Not to be confused with interfaces in a more general sense, the interface keyword creates an object that can be used to provide a common interface to classes without extending them as with abstract classes. Instead, the interface is implemented.

Interfaces differ from abstract classes. First, theyre actually classes. They do not define properties, and they do not define any behavior. Methods declared in an interface must be declared in classes that implement it.

Since the interface is more general, it is the definition of how the object interacts with other code, all methods must be declared public (see the section on visibility in this chapter). Using abstract classes, an abstract method can have any appearance, but expanding classes must use their implementation with the same (or weaker) appearance. The implementation of the interface adds methods as abstract methods for the object class, failure to implement it will lead to an error, for example, the following:

Fatal error: SomeConcreteClass class contains n abstract methods (methods) and must therefore be declared abstract or implement the remaining methods Yes, abstract classes can implement interfaces.

+8
Dec 09 '09 at 20:11
source share
Interfaces

are type declarations. type is a set of values, plus a set of operations that can be performed on them from the outside. the private method does not fit into this picture.

 interface T { public /*int*/ function f(array $a); } interface U { public /*T*/ function g(T $t); } class C implements U { public function g(T $t) { ... $x = $t->f(); ... } } 
Interfaces

useful because they describe, well, the interfaces of objects. how objects communicate with their environment.

Now let's say T::f can be declared private. how would this be useful for other objects? it will not be called from outside, it will not be part of its interface.

+6
Dec 09 '09 at 20:37
source share

In many cases, defining an interface helps other modules to guarantee the behavior and api of the class, in this case something personal to which other modules cannot receive or simply understand. This is why you can never use private methods for an interface.

+3
Nov 03 '16 at 19:22
source share



All Articles