Skip to content

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

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

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