Functorial interfaces

From: Date: Thu, 12 May 2016 16:37:28 +0000
Subject: Functorial interfaces
Groups: php.internals 
Request: Send a blank email to internals+get-93280@lists.php.net to get a copy of this message
Hi everybody, I hope this is the right place for asking such a question. If that's not the case, please excuse me; I'd appreciate if you could redirect me to the appropriate place. The PHP manual says, regarding Interfaces, that the class implementing the interface must use the exact same method signatures as are defined in the interface. Not doing so will result in a fatal error. I don't understand why this needs to be so. Consider for example the following case: I have an interface interface Foo {
    public function foo(SpecialClass $object);
} and a class class Bar {
    public function foo(BaseClass $object)
    {
        // do something with BaseClass $object
    }
} that has the same signature of Foo except for the fact that the method foo takes a BaseClass, where SpecialClass extends BaseClass. From a theoretical point of view, saying that a class satisfies the interface Foo means that it has a method foo that is able to deal with a SpecialClass object. So Bar satisfies the contract imposed by Foo since it has a method foo that is able to deal with a BaseClass, which means that it will also be able to deal with SpecialClass. Hence it would seem natural to say that Bar implements Foo. But, as the documentation says, this produces a fatal error. What is the reason for this fatal error? Is there a specific reason to do things this way? It this is the case, could you please provide an example that proves why my approach creates problems. Thank you

« previous php.internals (#93280) next »