-
Notifications
You must be signed in to change notification settings - Fork 11k
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
Resolve variadic parameters as an empty array #26950
Comments
I do not get any errors. |
I will update the issue with my php and laravel versions and how to reproduce the issue exactly |
Whats the usecase for a class that is resolved by the containee that injects a variadic parameter? |
I don't think making a constructor as you mentioned is a good idea. |
@koenhoeijmakers I am working on a project where I have a ...
class RuleBag implements \ArrayAccess, \Countable, \IteratorAggregate
{
protected $container = [];
// Of course rules can be set using a setter, but the class can
// be initialized with some initial rules.
public function __construct(Rule ...$rules)
{
$this->container = $rules;
}
...
} and then I am injecting this ...
class ProductValidator
{
protected $rules;
public function __construct(RuleBag $rules)
{
$this->rules = $rules;
}
...
} Now you obviously see that ...
class RuleBag implements \ArrayAccess, \Countable, \IteratorAggregate
{
protected $container = [];
public function __construct($rules = [])
{
// Manually validate that each element in the array is of type `Rule`
$this->validateRules($rules);
$this->container = $rules;
}
...
} |
@XuanQuynh Can you elaborate why you think so? cause in my opinion if you can resolve a class with this constructor ...
public function __construct($params = []);
... then it makes sense you can resolve a class with this constructor ...
public function __construct(Param ...$params);
... because |
I don't really want to dive into your example. I see no benefits this case. In fact, you may prefer Param ...$params to $params = []. I think it's your coding style. |
@XuanQuynh I am just saying that if you can resolve |
@aldemeery Ok. I see. For me, I just prefer the simpler way. If this is still helpful for you, you may persuade the core developers of Framework. |
Sadly, this is not a Laravel bug (those fixable quickly here) but core php: https://3v4l.org/E7Ysf |
@Korko thanks for the update. That's definitely something to consider. |
consider these two classes
now when calling
a
ReflectionException
will be thrown with a messageInternal error: Failed to retrieve the default value
while doing a
without passing any arguments will work just fine.
On the other hand if the constructor was like this:
calling
will also work just fine.
So, I am suggesting to add the ability to resolve variadic parameters as empty arrays
PHP version:
7.2
Laravel version:
5.7.16
.The text was updated successfully, but these errors were encountered: