Open Amoifr opened 2 years ago
Questions | Answers |
---|---|
Description? | New ADR for Variadic |
Type? | improvement |
BC breaks? | no |
Deprecations? | no |
Fixed ticket? | |
How to test? | |
Possible impacts? |
Even if it's PHP and I'm a front-end developer, I have to agree with this, no kind of types like in TS with Array<Products>
in PHP?
@NeOMakinG The best way is to pass a DTO as a parameter, but this is a workaround if you just have one dimension.
ADR added to README table https://github.com/PrestaShop/ADR/blob/master/README.md
It's kind of confusing since, according to your description, this operator can have 2 different meaning depending if it's typed or not, IF I understand well
function f($a, $b, ...$c) means that you can pass any number of arguments to the function, when it's more than 2, they will be in the variable $c
f(1, 2, $d, $e, $f, $g) => count($c) in the function will give 4
But with function f($a, $b, Product ...$c)
you cannot do f(1, 2, $d, $e, $f, $g) even if $d, $e, $f, $g are Product but you can do f(1, 2, [$d, $e, $f, $g])
I will look further to PHP documentation
Or maybe in your 2nd example f(Product ...$products) is not equivalent to f(array $products) ?
Even if it's PHP and I'm a front-end developer, I have to agree with this, no kind of types like in TS with
Array<Products>
in PHP?
Not yet unfortunately :/
In case BC break is an issue, I suggest a simple trick:
public function setItems(array $items): self
{
$this->items = [];
return $this->addItem(...$items);
}
protected function addItem(Item ...$items): self
{
foreach ($items as $item) {
$this->items[] = $item;
}
return $this;
}
It's kind of confusing since, according to your description, this operator can have 2 different meaning depending if it's typed or not, IF I understand well function f($a, $b, ...$c) means that you can pass any number of arguments to the function, when it's more than 2, they will be in the variable $c f(1, 2, $d, $e, $f, $g) => count($c) in the function will give 4 But with function f($a, $b, Product ...$c) you cannot do f(1, 2, $d, $e, $f, $g) even if $d, $e, $f, $g are Product but you can do f(1, 2, [$d, $e, $f, $g]) I will look further to PHP documentation
Or maybe in your 2nd example f(Product ...$products) is not equivalent to f(array $products) ?
Variadic arguments are always received as an array in the function body. This array contains as many items as variadic arguments provided.
If you need to pass items in an array as variadic arguments, just use the spread operator : f(1, 2, ...$products)
Let's try to push this topic this week so what we can vote next week \o/