Is it possible to have an interface that has private / protected methods? Is it possible to have an interface that has private / protected methods? php php

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


The PHP manual page about interfaces explicitly states:

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

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


Interfaces are used to describe public methods of a class implementing that interface. You can never have a private method in an interface. Any methods in an interface are assumed to be in use and should not be changed.

Interfaces is the PHP link, but this is standard in OO programming.


In general an interface can only have public members, because the only function of an interface is to be inherited.

From PHPfreaks.com tutorial:

PHP5 features interfaces. Not to be confused with interfaces in the more general sense, the interface keyword creates an entity that can be used to enforce a common interface upon classes without having to extend them like with abstract classes. Instead an interface is implemented.

Interfaces are different from abstract classes. For one, they’re not actually classes. They don’t define properties, and they don’t define any behaviour. The methods declared in an interface must be declared in classes that implement it.

Because an interface in the more general sense is a definition of how an object interacts with other code, all methods must be declared public (see section on visibility in this chapter). Using abstract classes, an abstract method can have any visibility, but the extending classes must have their implementations use the same (or weaker) visibility. Implementing an interface adds the methods as abstract methods to the subject class, failure to implement it will result in an error like the following:

Fatal error: Class SomeConcreteClass contains n abstract method(s) and must therefore be declared abstract or implement the remaining methodsYes, abstract classes can implement interfaces.