[PHP-BUG] Req #73435 [NEW]: Return type-hinting polymorphism

[email protected] ("php at haravikk dot me") Tue, 1 Nov 2016 17:31:21 GMT
Newsgroups php.standards
Message-ID <[email protected]>
From:             php at haravikk dot me
Operating system: 
PHP version:      7.0.12
Package:          PHP Language Specification
Bug Type:         Feature/Change Request
Bug description:Return type-hinting polymorphism

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 bug report at https://bugs.php.net/bug.php?id=73435&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=73435&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=73435&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=73435&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=73435&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=73435&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=73435&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=73435&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=73435&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=73435&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=73435&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=73435&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=73435&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=73435&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=73435&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=73435&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=73435&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=73435&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=73435&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=73435&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=73435&r=mysqlcfg