Bug #72442 [Nab->Sus]: Return type declarations and inheritance
Edit report at https://bugs.php.net/bug.php?id=72442&edit=1
ID: 72442
Updated by: cmb@php.net
Reported by: spam at bugyik dot cz
Summary: Return type declarations and inheritance
-Status: Not a bug
+Status: Suspended
Type: Bug
Package: Scripting Engine problem
Operating System: Ubuntu 16.04
PHP Version: 7.0.7
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Well, <https://bugs.php.net/71825> is about
parameters, but this
is about return types, so I'm re-opening, changing to feature
request and suspend the ticket, because it requires an RFC, see
<https://wiki.php.net/rfc/howto>.
Previous Comments:
------------------------------------------------------------------------
[2016-06-24 12:16:46] cmb@php.net
Related To: Bug #72483
------------------------------------------------------------------------
[2016-06-17 17:52:27] cmb@php.net
> The parameter must be an ==INSTANCEOF== the given class or
> interface name[2]
This section is about the types of the actual parameters (aka.
arguments), but not about formal parameters. The documentation
might be a bit sloppy there; feel free to file a separate
documentation bug report.
Anyhow, the behavior you're reporting as bug is prominently
documented[1]:
> When overriding a parent method, the child's method must match
> any return type declaration on the parent.
Currently, PHP implements invariant parameter and return types
only, i.e. no covariance or contravariance. There is already
request #71825 to add this feature.
[1] <http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration>
------------------------------------------------------------------------
[2016-06-17 14:56:39] spam at bugyik dot cz
Description:
------------
There is not possible to define return type parent object in abstract method,
Online demo: https://3v4l.org/innXW
From documentation:
The same types are available for return type declarations as are available for argument type
declarations[1]
The parameter must be an ==INSTANCEOF== the given class or interface name[2]
[1] http://php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
[2] http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.types
Test script:
---------------
<?php
abstract class Entity {}
class FooEntity extends Entity {}
abstract class Repository
{
abstract public function getDetail(): Entity;
}
class FooRepository extends Repository
{
public function getDetail(): FooEntity
{
return new FooEntity;
}
}
$obj = new FooRepository();
$obj->getDetail();
Expected result:
----------------
No error expected.
Actual result:
--------------
Fatal error: Declaration of FooRepository::getDetail(): FooEntity must be compatible with
Repository::getDetail(): Entity in /in/innXW on line 18
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=72442&edit=1
Thread (3 messages)