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

55,857

Solution 1

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

Solution 2

Interfaces are used to describe public methods of a class implementing that interface. You can never have a private method in an interface. Any methods in an interface are assumed to be in use and should not be changed.

Interfaces is the PHP link, but this is standard in OO programming.

Solution 3

In general an interface can only have public members, because the only function of an interface is to be inherited.

From PHPfreaks.com tutorial:

PHP5 features interfaces. Not to be confused with interfaces in the more general sense, the interface keyword creates an entity that can be used to enforce a common interface upon classes without having to extend them like with abstract classes. Instead an interface is implemented.

Interfaces are different from abstract classes. For one, they’re not actually classes. They don’t define properties, and they don’t define any behaviour. The methods declared in an interface must be declared in classes that implement it.

Because an interface in the more general sense is a definition of how an object interacts with other code, all methods must be declared public (see section on visibility in this chapter). Using abstract classes, an abstract method can have any visibility, but the extending classes must have their implementations use the same (or weaker) visibility. Implementing an interface adds the methods as abstract methods to the subject class, failure to implement it will result in an error like the following:

Fatal error: Class SomeConcreteClass contains n abstract method(s) and must therefore be declared abstract or implement the remaining methodsYes, abstract classes can implement interfaces.

Solution 4

interfaces are type declarations. a type is set of values, plus a set of operations that can be carried upon them from outside. a private method doesn't fit into this picture.

interface T {
  public /*int*/ function f(array $a);
}
interface U {
  public /*T*/ function g(T $t);
}

class C implements U {
    public function g(T $t) {
        ...
        $x = $t->f();
        ...
    }
}

interfaces are useful because they state, well, objects' interfaces. how the objects communicate with their environment.

now let's say T::f could be declared private. how would that be useful to other objects? it would not callable from outside, it would not be part of its interface.

Solution 5

In many cases, an interface definition helps other modules guarantee the behavior and the API of a class. In those cases, private methods are not something the other modules can access or understand. That's why you can never put private methods on an interface.

Share:
55,857

Related videos on Youtube

teepusink
Author by

teepusink

Updated on October 07, 2020

Comments

  • teepusink
    teepusink over 3 years

    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.

    • Stoutie
      Stoutie over 11 years
      I find the answer disappointing. I would like interfaces that support protected/private methods as well. For example, I have a class, where a public method, implemented in the abstract, relies on a protected method implemented by subclass. I want to use an interface to require subclasses to implement the protected methods required by the abstract public methods.
    • Stijn de Witt
      Stijn de Witt about 10 years
      Use an abstract base class for that purpose. You can combine the two approaches: public methods in the interface, implementation of those methods in an abstract base class that defines (and relies on) abstract protected methods.
    • tvanc
      tvanc about 10 years
      If you could declare private or protected methods, it would be private function method1 (); not private method1();.
  • Stoutie
    Stoutie over 11 years
    It's too bad. Because I would like to have a public method, required by the interface, implemented in an abstract class, that relies on a protected method also enforced by the interface. This way, the abstract class can provide the public interface, but it's up to subclasses to implement the underlying logic. Make sense?
  • Sven
    Sven over 11 years
    Seems like you want the method to be implemented by the subclass to be abstract. Then any subclass MUST implement it. But it has nothing to do with the interface.
  • User123456
    User123456 almost 7 years
    in other languages like java , you could use access modifiers in interfaces.
  • asdfasdfasdf
    asdfasdfasdf over 3 years
    Agreed... But it could be protected right? As abstract classes allow...