در حال بارگذاری ...

Recap: variable arguments0:00

Next up, we can now use the spread operator within arrays. But first, a recap. Imagine you have the typical add function that will add x and y. And of course, we can run it like so. And this will return 5. Basic stuff. Now, if you want to accept a variable number of arguments, like x, y, and z, well, in the past, in the old days, you would end up using something like func_get_args. This will return an array of all the arguments you pass to the function. So then you could pass that to array_sum, and you'll get the same thing.

This will return an array of all the arguments you pass to the function. So then you could pass that to array_sum, and you'll get the same thing. And now you can pass as many arguments as you want. This will return 9. Okay, cool. But of course, there are issues like if you pass another array, or a boolean, or a string there, you may not get what you expect. In this case, it disregards them. Okay. So one option is to use variadic functions, which again is confusing.

Using variadic functions0:56

Okay. So one option is to use variadic functions, which again is confusing. Just think of it as a function that accepts a variable number of arguments. So we could refactor to this. Now we don't have to make a function call. We can rely on a language constructor structure. So have a look. We give it a run again, and we still get 9. So we still have this issue here. However, we could then add a type to it, which means every item within this array should

Introducing spread operator1:18

So we still have this issue here. However, we could then add a type to it, which means every item within this array should be of type integer. Now if we give it a run, we get a fatal error, which is what we want in this case. We'd have to fix the issue. And now it works again. So again, this is true for 7.4 and 7.3 and below. However, what's new in php 7.4 is the ability to use the spread operator, or you might hear it, by the way, referred to as the splat operator in other languages. Like in Ruby, they'll call this the splat operator.

Spread operator caveats2:43

Just unpack 1 and 2 into items, 3 and 4, and 6 and 7. And now you have a single level array. And this is what you want. But a quick caveat, if you have an associative array, be careful, you can't use string keys. So if you had something like that, it's not going to work like you expect. It's going to throw an error. Cannot unpack arrays with string keys. So that is something to be aware of. But yeah, this will work in PHP 7.4, and it will fail in 7.3. So to finish up with our silly add function, maybe you start with a current number like

Refactoring add with spread3:09

But yeah, this will work in php 7.4, and it will fail in 7.3. So to finish up with our silly add function, maybe you start with a current number like the initial. And then you accept a variable number of integers to add to it. So now you could call it like so. add and maybe the initial number is 3, but then I want to add 4 to it and then 3 to it. And that should create 10. So how would you do this? Well, you might think, well, we have our numbers, let's just push current on top of it and then get the sum of it like so.

Well, you might think, well, we have our numbers, let's just push current on top of it and then get the sum of it like so. So you give that a run and you get 10. And that, of course, is an option. But alternatively, we could do this now in php 7.4. We're going to start with the current number and then unpack the list of numbers you provide. And we pass that to array_sum. So now if we run it again, we get the same thing. But of course, in php 7.3 and below, this code would throw an error.

But of course, in PHP 7.3 and below, this code would throw an error.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟