Bug #79482 [Nab->Opn]: Contravariance issue

From: Date: Thu, 16 Apr 2020 14:16:52 +0000
Subject: Bug #79482 [Nab->Opn]: Contravariance issue
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226619@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79482&edit=1 ID: 79482 Updated by: derick@php.net Reported by: john at designermonkey dot co dot uk Summary: Contravariance issue -Status: Not a bug +Status: Open Type: Bug Package: Unknown/Other Function Operating System: n/a PHP Version: Irrelevant Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Function arguments are contra-variant, not co-variant, meaning that an overloaded/inherited method can chose to accept a broader type. If it would allow for a more specific/narrow type, then an already existing bit of code could no longer call the overloaded with the same method arguments as the original parent class. So this is not a bug, but an implication of the Barbara Liskov Substitution Principle. Previous Comments: ------------------------------------------------------------------------ [2020-04-16 14:15:04] pmmaga@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php You can read the details on the proposal: https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters The parameters are contravariant, not covariant ------------------------------------------------------------------------ [2020-04-16 14:09:39] john at designermonkey dot co dot uk Description: ------------ We now have contravariance and covariance. With covariance, we can substitute a return type of object with a more specific type like MyCustomClass for example. We cannot substitute object with MyCustomClass as a function argument typehint though. I feel this is a bug. Why would it be allowed as a return type but not a typehint? Test script: --------------- <?php class MyClass {} // ---------- interface TestCo { public function testCo(): object; } class TestCoImplementation implements TestCo { public function testCo(): MyClass { return new MyClass; } } $test = new TestCoImplementation; $object = $test->testCo(); var_dump($object); // ---------- interface TestCon { public function testCon(object $object): object; } class TestConImplementation implements TestCon { public function testCon(MyClass $object): object { return $object; } } $test = new TestCoImplementation; $object = $test->testCo(new MyClass); var_dump($object); Expected result: ---------------- I would hope that the Fatal Error is not present and that the second var_dump provides my class instance. Actual result: -------------- /Users/john/Desktop/contract_test.php:25: class MyClass#2 (0) { } PHP Fatal error: Declaration of TestConImplementation::testCon(MyClass $object): object must be compatible with TestCon::testCon(object $object): object in /Users/john/Desktop/contract_test.php on line 37 PHP Stack trace: PHP 1. {main}() /Users/john/Desktop/contract_test.php:0 Fatal error: Declaration of TestConImplementation::testCon(MyClass $object): object must be compatible with TestCon::testCon(object $object): object in /Users/john/Desktop/contract_test.php on line 37 Call Stack: 0.0026 399984 1. {main}() /Users/john/Desktop/contract_test.php:0 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79482&edit=1

« previous php.bugs (#226619) next »