Skip to content

[9.x] Primitive variadic class make#43985

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
Tofandel:patch-2
Sep 5, 2022
Merged

[9.x] Primitive variadic class make#43985
taylorotwell merged 1 commit into
laravel:9.xfrom
Tofandel:patch-2

Conversation

@Tofandel

@Tofandel Tofandel commented Sep 2, 2022

Copy link
Copy Markdown
Contributor

Currently the following code

class Test { 
    protected array $parameters = []; 
    public function __construct(...$parameters) { 
        $this->parameters = $parameters; 
    }
}
>>> app()->make(Test::class);

Will throw Illuminate\Contracts\Container\BindingResolutionException with message 'Unresolvable dependency resolving [Parameter #0 [ <optional> ...$parameters ]] in class Test'

Even though Test is a perfectly instantiable class without any parameters (eg: new Test();)

That is because php doesn't mark variadic parameters as having default values in Reflection (even though they technically do as they are always arrays) and laravel only checks for $parameter->isDefaultValueAvailable() instead of $parameter->isOptional() before throwing an exception

Because laravel uses make for almost any class we provide it, this makes us unable to use this kind of class definition

Fixes #26950

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Resolve variadic parameters as an empty array

2 participants