Skip to content

When an interface extends multiple parent interfaces, the order of extends statements affects variance compatibility parsing #25759

Backlog

Description

@bitwise-operators

Description

The following code:

<?php
interface HasOptionalValue {
    public function getValue(): ?int;
}

interface HasValue {
    public function getValue(): int;
}

interface Result extends HasOptionalValue, HasValue {
}

Resulted in this output:

Fatal error: Declaration of HasOptionalValue::getValue(): ?int must be compatible with HasValue::getValue(): int

When you invert the order of the extends statements like this:

<?php
interface Result extends HasValue, HasOptionalValue {
}

The code parses without problems.

So basically, the most restrictive interface needs to go first.

I couldn't find anything in the documentation that mentions of order of statements, so I would consider this a bug.

My expected behaviour would be that of the second example: the child interface is considered to have the smallest intersection of the two interfaces.

By the way: adding the most restrictive function declaration to the child interface will also fix the problem, but this is not always desirable as it complicates changes to the parent interface.

PHP Version

phpinfo()
PHP Version => 8.6.0

System => Linux ip-172-31-20-248.eu-central-1.compute.internal 6.1.188-233.393.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Oct  6 20:31:38 UTC 2026 x86_64
Build Date => Nov 20 2026 13:54:37
Build System => Linux ip-172-31-21-217.eu-central-1.compute.internal 6.1.166-197.305.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Mon Mar 23 09:53:26 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Configure Command =>  './configure'  '--with-libdir=lib64' '--without-libxml' '--disable-dom' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--without-openssl' '--disable-simplexml' '--without-sqlite3' '--without-pdo-sqlite' '--enable-fpm' '--enable-calendar' '--with-pecl' '--without-rpath' '--enable-bcmath' '--enable-shmop' '--enable-sysvsem' '--enable-inline-optimization' '--with-curl' '--enable-mbregex' '--enable-mbstring' '--enable-intl' '--with-mcrypt' '--with-libmbfl' '--enable-ftp' '--with-mhash' '--enable-pcntl' '--enable-sockets' '--enable-zip' '--with-zip' '--with-gettext' '--without-fileinfo' '--enable-opcache' '--with-ldap=shared' '--with-config-file-path=/home/phpversions/etc/php.8/' '--with-config-file-scan-dir=/home/phpversions/etc/php.d/' '--prefix=/home/phpversions/php/8.6.0'
Server API => Command Line Interface

Operating System

No response

Activity

  1. DanielEScherzer commented on Nov 26, 2026

    @DanielEScherzer
    Member

    This has been the case since PHP 7.1 when https://wiki.php.net/rfc/nullable_types was implemented
    Compare https://3v4l.org/lFKpJ#veol with https://3v4l.org/g6AfC#veol

  2. bitwise-operators commented on Nov 26, 2026

    @bitwise-operators
    Author

    Yeah, it's not exactly a common issue.
    It also applies to tightening of interfaces by specifying child classes (not just nullability)

    The situation where I encountered it actually looked more like this:

    interface Animal {}
    interface Cat extends Animal {}
    
    interface PersonWithAnimal {
        public function getAnimal(): Animal;
    }
    interface PersonWithCat extends PersonWithAnimal {
        public function getAnimal(): Cat;
    }
    
    interface PetOwner extends PersonWithAnimal {}
    interface CatOwner extends PetOwner, PersonWithCat {}

    In this case, the CatOwner interface needs to have the PersonWithCat interface first, or the error occurs.

    This is not exactly a high priority issue, as it is an edge-case and has several workarounds, but the fact this codestyler exists tells me this is not expected behaviour.

  3. added a commit that references this issue on Nov 26, 2026
    5ae1a30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions