Search code examples
phpoopinterface

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.


Solution

  • 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 ;-)