|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72442 Return type declarations and inheritance
Submitted: 2016-06-17 14:56 UTC Modified: 2017-08-16 16:29 UTC
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: spam at bugyik dot cz Assigned:
Status: Suspended Package: Scripting Engine problem
PHP Version: 7.0.7 OS: Ubuntu 16.04
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: spam at bugyik dot cz
New email:
PHP Version: OS:


 [2016-06-17 14:56 UTC] spam at bugyik dot cz
There is not possible to define return type parent object in abstract method,

Online demo:

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]



Test script:

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();

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-06-17 17:52 UTC]
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2016-06-17 17:52 UTC]
> 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

> 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] <>
 [2016-09-23 18:08 UTC]
-Status: Not a bug +Status: Suspended
 [2016-09-23 18:08 UTC]
Well, <> 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
 [2016-09-23 18:08 UTC]
-Type: Bug +Type: Feature/Change Request
 [2017-08-16 16:29 UTC]
-Assigned To: cmb +Assigned To:
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 23:01:29 2024 UTC