Skip to content

null default-value parameter before required parameter is silently ignored #11485

Closed as not planned
@syranide

Description

@syranide

Description

The following code:

<?php
function foobar(
    string|null $a = null,
    string $b
) {}

foobar(b: "x");

Resulted in this output:

Fatal error: Uncaught ArgumentCountError: foobar(): Argument #1 ($a) not passed

But I expected this error instead:

Deprecated: Optional parameter $a declared before required parameter $b is implicitly treated as a required parameter

To explain; $a does not end up being optional, and if you perform reflection on the property, it does not have a default-value. So PHP is silently ignoring the user-specified behavior. While it seems that PHP considers having optional properties in-front of required properties as invalid, it should at least issue a warning instead of silently changing expectations. If you put a string as default-value instead, it will emit the above depreciation notice.

PHP Version

PHP 8.2.7

Operating System

No response

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