Skip to content

ReflectionParameter::isDefualtValueAvailable() always returns false if parameters are out of order #17676

@Crell

Description

@Crell

Description

The following code:

<?php

declare(strict_types=1);

class Dummy
{
    public function __construct(public bool $routable = true, public int $junk)
    {
    }
}

$rConstructor = new \ReflectionClass(Dummy::class)->getConstructor();

$rParams = $rConstructor->getParameters();

var_dump($rParams[0]->isDefaultValueAvailable());

Resulted in this output:

bool(false)

But I expected this output instead:

bool(true)

More precisely, it seems that when a required parameter comes after an optional parameter, something in reflection goofs and it isn't able to see the default value for the optional parameter.

I suspect this hasn't been run into before, because it's so rare to have a constructor that has its parameters backwards. It doesn't actually error for me, as I'd expect it to, but the reflection gets goofy as shown above. (In my case I'm only creating the object via reflection, so didn't even realize I had the parameters in a bad order.)

PHP Version

8.4.2

Operating System

Ubuntu 22.04

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