Bug #70722 [Opn]: self return type in interface does not behave as expected

From: Date: Fri, 16 Oct 2015 00:29:03 +0000
Subject: Bug #70722 [Opn]: self return type in interface does not behave as expected
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196634@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70722&edit=1 ID: 70722 User updated by: eric at ericstern dot com Reported by: eric at ericstern dot com Summary: self return type in interface does not behave as expected Status: Open Type: Bug -Package: Compile Failure +Package: Scripting Engine problem Operating System: OS X PHP Version: 7.0.0RC5 Block user comment: N Private report: N New Comment: Changed package; I'm assuming the compiler package is compiling PHP itself, rather than PHP processing a script Previous Comments: ------------------------------------------------------------------------ [2015-10-16 00:27:19] eric at ericstern dot com 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 this bug report at https://bugs.php.net/bug.php?id=70722&edit=1

« previous php.bugs (#196634) next »