Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Primitive variadic class make #43985

Merged
merged 1 commit into from
Sep 5, 2022
Merged

[9.x] Primitive variadic class make #43985

merged 1 commit into from
Sep 5, 2022

Conversation

Tofandel
Copy link
Contributor

@Tofandel Tofandel commented Sep 2, 2022

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