Req #78807 [Opn->Nab]: Method signature types strictly enforced - should allow subclasses

From: Date: Wed, 13 Nov 2019 04:16:06 +0000
Subject: Req #78807 [Opn->Nab]: Method signature types strictly enforced - should allow subclasses
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-223691@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78807&edit=1 ID: 78807 Updated by: requinix@php.net Reported by: mlambley at gmail dot com Summary: Method signature types strictly enforced - should allow subclasses -Status: Open +Status: Not a bug Type: Feature/Change Request -Package: PHP Language Specification +Package: *General Issues Operating System: All PHP Version: 7.3.11 Block user comment: N Private report: N New Comment: https://en.wikipedia.org/wiki/Liskov_substitution_principle https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) If A::fn accepts any class of type B, then SubA::fn must also accept any class of type B. That is called contravariance. Previous Comments: ------------------------------------------------------------------------ [2019-11-13 03:31:44] mlambley at gmail dot com Description: ------------ Not sure if this is a bug or a feature request, but the example code I have posted produces a warning: Warning: Declaration of SubA::fn(SubB $b) should be compatible with A::fn(B $b) SubB is a subclass of B, so the method override in class SubA should be valid. I would expect the behaviour to be similar to how instanceof works: var_dump((new SubB()) instanceof B); //true That line of code returns true, and it should, because of OOP. I understand that warnings are not errors, but when I'm publishing modules which may be used by the general public it is impractical to tell them that they must disable warnings. Is it possible for PHP to respect subclasses in typed method signatures, or will this have unforeseen behaviours I haven't anticipated? Thank you. Test script: --------------- <?php class A { public function fn(B $b) {} } class SubA extends A { public function fn(SubB $b) { parent::fn($b); } } class B {} class SubB extends B {} $obj = new SubA(); $obj->fn(new SubB()); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78807&edit=1

« previous php.bugs (#223691) next »