Bug #79482 [Opn->Nab]: Contravariance issue
| From: | pmmaga@php.net | Date: | Thu, 16 Apr 2020 14:15:04 +0000 |
| Subject: | Bug #79482 [Opn->Nab]: Contravariance issue | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-226618@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: pmmaga@php.net
Reported by: john at designermonkey dot co dot uk
Summary: Contravariance issue
-Status: Open
+Status: Not a bug
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
You can read the details on the proposal: https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters
The parameters are contravariant, not covariant
Previous Comments:
------------------------------------------------------------------------
[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