Skip to content

Override construct function with optional argument should not produce fatal error #23630

Open

Description

@Renji-FR

Description

The following code:

<?php
    interface MyInterface1 { public function __construct(?string $name = null); }
    interface MyInterface2 { public function __construct(?string $name = null, ?int $id = null); }

    interface MyInterface3 extends MyInterface1, MyInterface2 { }

Resulted in this output:

PHP Fatal error:  Declaration of MyInterface1::__construct(?string $name = null) must be compatible with MyInterface2::__construct(?string $name = null, ?int $id = null) in php shell code on line 1

But I expected no error and the following code works as expected:

<?php
    interface MyInterface1 { public function __construct(?string $name = null); }
    interface MyInterface2 { public function __construct(?string $name = null, ?int $id = null); }

    interface MyInterface4 extends MyInterface2, MyInterface1 { }

PHP Version

PHP 8.4.24 (cli) (built: Jul 30 2026 15:23:13) (NTS)
Copyright (c) The PHP Group
Built by Ubuntu
Zend Engine v4.4.24, Copyright (c) Zend Technologies
    with Zend OPcache v8.4.24, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04.4 LTS

Activity

  1. lazerg commented on Sep 9, 2026

    @lazerg
    Contributor

    It is a bug. When an interface doesn't declare a method itself, the engine copies the declaration from the first parent listed and only checks that copy against the later ones, so it only links if the interface you list first already has the most specific signature. Same root cause as bug #76361.

    PR is up: #23638.

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