Bug #70722 [Opn->Nab]: self return type in interface does not behave as expected
Edit report at https://bugs.php.net/bug.php?id=70722&edit=1
ID: 70722
Updated by: requinix@php.net
Reported by: eric at ericstern dot com
Summary: self return type in interface does not behave as
expected
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: OS X
PHP Version: 7.0.0RC5
Block user comment: N
Private report: N
New Comment:
The RFC also mentions that the return type must be identical.
> The enforcement of the declared return type during inheritance is invariant;
> this means that when a sub-type overrides a parent method then the return type
> of the child must exactly match the parent
and then
> Covariant return types are considered to be type sound and are used in many
> other languages. This RFC originally proposed covariant return types but was
> changed to invariant because of a few issues. It is possible to add covariant
> return types at some point in the future.
So for now the return type must be Chainable; specifying "Chainable" rather than
"self" can help you with that.
Previous Comments:
------------------------------------------------------------------------
[2015-10-16 00:29:02] eric at ericstern dot com
Changed package; I'm assuming the compiler package is compiling PHP itself, rather than PHP
processing a script
------------------------------------------------------------------------
[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
Thread (3 messages)