|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80126 Covariant return types failing compilation
Submitted: 2020-09-21 01:42 UTC Modified: 2020-10-06 14:21 UTC
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: terence-marks at zencontrol dot com Assigned: nikic (profile)
Status: Closed Package: Compile Failure
PHP Version: 7.4.10 OS: Debian Buster
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: terence-marks at zencontrol dot com
New email:
PHP Version: OS:


 [2020-09-21 01:42 UTC] terence-marks at zencontrol dot com
Discovered an issue with narrowing return type using covariance.

I have a chain of inheritance (...ignore implementation details) from which at the lowest level I attempt to narrow the return type for an inherited method. When running the code PHP fails at compilation stating the method is not compatible.

Now the weird part is it seems the covariance check passes if I remove all interfaces from the lowest level (see example).

The attached test script should point you guys in the right direction.


Test script:

interface Poo
    public function poo(): Poo;

abstract class AbstractPoo implements Poo
    public function poo(): Poo
        return new static();

class ConcretePoo extends AbstractPoo

interface B

// Fails if interface is present
class ExtendedConcretePoo extends ConcretePoo implements B
    public function poo(): ExtendedConcretePoo
        return new static();

// Does not fail if no interfaces present
//class ExtendedConcretePoo extends ConcretePoo
//    public function poo(): ExtendedConcretePoo
//    {
//        return new static();
//    }

$e = new ExtendedConcretePoo();
$r = $e->poo();

Expected result:
No compilation errors.

Actual result:
Declaration of ExtendedConcretePoo::poo(): ExtendedConcretePoo must be compatible with AbstractPoo::poo()


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-21 12:32 UTC]
I don't think the extra interface is necessary to show the bug. This seems to fail also..

interface inty {
    public function intmethod(): inty;

abstract class AbstractClass implements inty {
    public function intmethod(): inty
        return new static();

class Concrete extends AbstractClass { }

class ExtendedConcrete extends Concrete {
    // fails
    public function intmethod(): ExtendedConcrete {
        return new static();

    // Works
//    public function intmethod(): Concrete {
//        return new static();
//    }
 [2020-10-06 14:21 UTC]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2020-10-06 14:21 UTC]
@danack's example is a PHP 8 regressions, likely caused by a change in interface inheritance order.
 [2020-10-06 14:34 UTC]
Automatic comment on behalf of
Log: Fix bug #80126
 [2020-10-06 14:34 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Thu Dec 09 01:03:34 2021 UTC