Bug #79482 [NEW]: Contravariance issue

From: Date: Thu, 16 Apr 2020 14:09:39 +0000
Subject: Bug #79482 [NEW]: Contravariance issue
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226617@lists.php.net to get a copy of this message
From: john at designermonkey dot co dot uk Operating system: n/a PHP version: Irrelevant Package: Unknown/Other Function Bug Type: Bug Bug description:Contravariance issue 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 bug report at https://bugs.php.net/bug.php?id=79482&edit=1 -- Fix committed: https://bugs.php.net/fix.php?id=79482&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=79482&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=79482&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=79482&r=needscript Try newer version: https://bugs.php.net/fix.php?id=79482&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=79482&r=support Expected behavior: https://bugs.php.net/fix.php?id=79482&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=79482&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=79482&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=79482&r=globals PHP version support discontinued: https://bugs.php.net/fix.php?id=79482&r=phptooold Daylight Savings: https://bugs.php.net/fix.php?id=79482&r=dst IIS Stability: https://bugs.php.net/fix.php?id=79482&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=79482&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=79482&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=79482&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=79482&r=mysqlcfg

« previous php.bugs (#226617) next »