I’m making use of an interface for a set of classes. I have a problem however because I wish for any visibility to be allowed in the interface (That is: public, protected and private).
I need the parent method to only be protected and I need the child method to be private, but I get the error saying
Fatal error: Access type for interface method Baz::qux() must be omitted in <the file with Baz/Bar>.”
I tried specifying other visibility methods in the inteface Baz and removing public, but they all failed.
Is there a way I can do it via the interface? If not, then is there a way I can declare it abstract, I tried that as well, but failed.
interface Baz
{
public function qux();
}
class Bar implements Baz
{
protected function qux()
{
//do foo
}
}
class Foo extends Bar implements Baz
{
private function qux()
{
parent::qux();
}
}
Methods you declare in Interfaces should be public. You define a contract with an interface. Any non-public methods would be implementation details and those do not belong into an Interface. As the name implies implementation details should go into the concrete classes implementing the interface.
From Wikipedia: