Bug #70722 [NEW]: self return type in interface does not behave as expected
| From: | eric at ericstern dot com | Date: | Fri, 16 Oct 2015 00:27:20 +0000 |
| Subject: | Bug #70722 [NEW]: self return type in interface does not behave as expected | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-196633@lists.php.net to get a copy of this message | ||
From: eric at ericstern dot com
Operating system: OS X
PHP version: 7.0.0RC5
Package: Compile Failure
Bug Type: Bug
Bug description:self return type in interface does not behave as expected
Description:
------------
When implementing an interface where a method is typehinted to return
self, the return type in the implementing class must explicitly name
the interface, rather than being allowed to use self as the typehint
in the implementing class.
While the RFC (https://wiki.php.net/rfc/return_types) calls out hinting
a child class as invalid, self on an implementing class seems like it
should be valid, as self in the implementing class will, by
definition, be an instance of the interface which is being implemented.
Also, in practice, it makes writing out the code to implement the
interface very tedious.
Test script:
---------------
// Does not work, unexpected:
<?php
interface Chainable {
public function something(): self;
}
class MyChainable implements Chainable {
public function something(): self {
return $this;
}
}
$x = new MyChainable();
print_r($x->something());
// Currently works (expected)
<?php
interface Chainable {
public function something(): self;
}
class MyChainable implements Chainable {
public function something(): Chainable{
return $this;
}
}
$x = new MyChainable();
print_r($x->something());
Expected result:
----------------
MyChainable Object
(
)
Actual result:
--------------
PHP Fatal error: Declaration of MyChainable::something(): MyChainable
must be compatible with Chainable::something(): Chainable in
/Users/firehed/dev/7/ret.php on line 9
--
Edit bug report at https://bugs.php.net/bug.php?id=70722&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70722&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70722&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70722&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70722&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70722&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70722&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70722&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70722&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70722&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70722&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70722&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70722&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70722&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70722&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70722&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70722&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70722&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70722&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70722&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70722&r=mysqlcfg