Sunday 3 June 2018

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


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...