What does “implements” do on a class?

前端 未结 7 839
抹茶落季
抹茶落季 2020-12-22 18:51

If a class implements another class... what does that mean? I found this code sample: http://www.java2s.com/Code/Php/Class/extendsandimplement.htm

But unfortunately

7条回答
  •  醉酒成梦
    2020-12-22 19:14

    Interfaces are implemented through classes. They are purely abstract classes, if you will.

    In PHP when a class implements from an interface, the methods defined in that interface are to be strictly followed. When a class inherits from a parent class, method parameters may be altered. That is not the case for interfaces:

    interface ImplementMeStrictly {
       public function foo($a, $b);
    }
    
    class Obedient implements ImplementMeStrictly {
       public function foo($a, $b, $c) 
          {
          }
    }
    

    will cause an error, because the interface wasn't implemented as defined. Whereas:

    class InheritMeLoosely {
       public function foo($a)
          {
          }
    }
    
    class IDoWhateverWithFoo extends InheritMeLoosely {
       public function foo()
          {
          }
    }
    

    Is allowed.

提交回复
热议问题