teepusink teepusink - 28 days ago 10
PHP Question

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

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

Right now I have:

interface iService
{
private method1();
}


That throws an error:


Parse error: syntax error, unexpected T_STRING, expecting T_VARIABLE


I just want to have confirmation that it is the case that an interface can only contain public methods.

Answer

The PHP manual page about interfaces explicitely 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 ;-)

Comments