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

后端 未结 7 1125
逝去的感伤
逝去的感伤 2021-02-01 12:00

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

Right now I have:

interface iService
{
    private method1();
}
         


        
7条回答
  •  粉色の甜心
    2021-02-01 12:13

    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.

提交回复
热议问题