36

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();
    }
}
Peyman Mohamadpour
  • 17,954
  • 24
  • 89
  • 100
TheFrack
  • 2,823
  • 7
  • 28
  • 47
  • 3
    Whenever you seem to need such a thing, that is usually a strong indication that something is wrong with your concept. – Niko Oct 15 '12 at 15:08
  • My apologies, now I know this. Thanks for the info all. – TheFrack Oct 15 '12 at 15:15
  • An interface is just a wireframe so why you care for visibility in it – Ertunç Oct 15 '12 at 15:08
  • PHP raises same error when you specify method as `abstract`. For instance: `abstract public function qux();` –  Aug 26 '13 at 11:13

3 Answers3

59

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:

Programming to the interface

The use of interfaces allows a programming style called programming to the interface. The idea behind this is to base programming logic on the interfaces of the objects used rather than on internal implementation details. Programming to the interface reduces dependency on implementation specifics and makes code more reusable.[7] It gives the programmer the ability to later change the behavior of the system by simply swapping the object used with another implementing the same interface.

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 1
    I have a `__call()` method that calls protected method with the same name of property with prefix. Access to `$object->name` calls protected `$object->getName()`. In this case, interface of private or protected method might have some sense? I'm not sure. – Ivan Montilla Apr 21 '17 at 08:16
  • 1
    @IvanMontilla an interface for said usage would not make sense, as the interface is aimed towards external usage of any class implementing it. If you want to force consistent, inherited behaviour in the form of protected methods you can use an abstract class with an abstract method, forcing all classes extending that abstract class to implement said method (as long as these classes are not abstract themselves). (This is also what JvdBerg means with his [answer](http://stackoverflow.com/a/12898500/1645553) to this question.) – Michiel Bakker Apr 21 '17 at 09:52
18

A interface is a contract between 2 parties, a agreement how they communicate.

It makes no sense to make methods protected or private, because the other party will not see those.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
0

The devs disabled the visibility for more fluid reusage. Through the keyword implements you already bind an interface to a class. You cannot access an interface without having it implemented anyways.

Thielicious
  • 4,122
  • 2
  • 25
  • 35