Req #73435 [Opn->Dup]: Return type-hinting polymorphism
| From: | requinix@php.net | Date: | Tue, 01 Nov 2016 17:39:43 +0000 |
| Subject: | Req #73435 [Opn->Dup]: Return type-hinting polymorphism | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-205123@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73435&edit=1
ID: 73435
Updated by: requinix@php.net
Reported by: php at haravikk dot me
Summary: Return type-hinting polymorphism
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
-Package: PHP Language Specification
+Package: Scripting Engine problem
PHP Version: 7.0.12
Block user comment: N
Private report: N
New Comment:
Duplicate of request #72442.
Previous Comments:
------------------------------------------------------------------------
[2016-11-01 17:31:16] php at haravikk dot me
Description:
------------
I'd like to propose that when implementing/extending that type-hints for return values can be
fulfilled by any sub-class of the same type.
In other words, sub-classes, when implementing/extending a method, should be able to provide a
stricter type-hint for their return value, so long as it is a sub-class of the parent's
type-hint.
Please see the test script for an example; in it I would expect to be able to provide a return
type-hint of "Bar", as it is a sub-type of "Foo", therefore meeting the original
requirements while clarifying that the sub-class is more specific in what specific flavour of
"Foo" it will actually return.
There is a similar issue (#60107) in which the Liskov Substitution Principle is referenced, however,
while allowing this behaviour on method arguments might breach LSP, I believe doing so only for
return types should be okay.
Test script:
---------------
class Foo {}
class Bar extends Foo {}
abstract class MyType {
abstract function example(): Foo;
}
class MyTypeImpl extends MyType {
function example(): Bar { /* Do something here */ }
}
Expected result:
----------------
The implementation of the example method by "MyTypeImpl" should be accepted as a valid
implementation of the method in "MyType".
Actual result:
--------------
Fatal error: Declaration of MyTypeImpl::example(Bar $value) must be compatible with
MyType::example(Foo $value) in test.php on line 12
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73435&edit=1