Setup and Comparisons0:00
Let's have a look at what's new in PHP 7.4. But very quickly, for some setup, I have two installations of PHP installed, one for PHP 7.4.5, and another for PHP 7.3, and this way we can compare the results. Next, in my composer.json file, the only thing I pulled in is illuminate/support, so that I can use collections in a number of the episodes. Alright, let's get started. First up is arrow functions, which I'm very happy about. So let's create, as an example, a Series class. And this will consist of a title alone, and we'll make that public. OK, so back in index.php, for setup, let's create a collection of series, and that'll
And this will consist of a title alone, and we'll make that public. OK, so back in index.php, for setup, let's create a collection of series, and that'll take the shape of something like this. Maybe a PHP series, and then another one. Alright, so pretty standard stuff so far. Let's now say, programmatically, we need to make the titles uppercase. Well, you could do a foreach, or an each method on the collection instance, and again that'll look something like this. So you might say the title equals ucwords(title). And yeah, if I were to now var_dump(series), and if we take a look at it, and again we
Introducing Arrow Functions1:06
So you might say the title equals ucwords(title). And yeah, if I were to now var_dump($series), and if we take a look at it, and again we get our collection, but now the titles are capital. OK, so in situations like this, where you have a closure with a simple expression within it, if you'd like, you can swap these out with arrow functions, or short closures, if you'd like to call them that. And that'll look something like this. We're going to use this new fn symbol, where we update the title. So this and this are functionally identical. But of course, if we were to run it, it works in 7.4, and it fails in 7.3.
Arrow Function Limitations1:40
So this and this are functionally identical. But of course, if we were to run it, it works in 7.4, and it fails in 7.3. We get a parse error. All right, now a few things to be aware of here. If you come from the JavaScript world, you might expect to have multiline short functions. And that'll look something like this. But yeah, as you can see by the error here, it's not going to work in php. You're limited to single line short functions. So I'll bring that back. Next, notice how the return keyword is implicit.
Implicit Return with Map2:08
So I'll bring that back. Next, notice how the return keyword is implicit. So as another example, let's say I want to pluck the title. Well, again, with Laravel's collections, we could just do pluck('title'). But let's instead do it with map. So traditionally, you would do something like this. You would return the title, and that will create a brand new collection. So if I now run that, you get a collection of only the titles there. But again, with short functions, you could rewrite this like so: series()->map(function($item) { return $item->title; }).
Arrow Function Scope Capture2:35
But again, with short functions, you could rewrite this like so. Series, map, and then we just want the title there. So again, this and this are functionally identical. It'll work in PHP 7.4, and it'll fail in 7.3. All right, last thing, let's talk about function scope. So for this, let's create maybe a SeriesCollection class. And this will extend Laravel's Collection class. All right, so maybe, I don't know, we want some kind of find or first by title. And again, there's a variety of built-in helpers. But let's say you didn't have access to that.
And again, there's a variety of built-in helpers. But let's say you didn't have access to that. You might use the first function, accept the series, and you're going to check if the series title matches up with what we pass in here. So you know this drill. You want to access scope outside of the function, so you have to use it explicitly, which is always annoying. But nonetheless, this will look in the collection and find the first series where the title matches up with what you provide. So if we were to test this out, let's start from scratch, and this will now be a new series
matches up with what you provide. So if we were to test this out, let's start from scratch, and this will now be a new series collection. All right, so now if we were to var_dump the first by title, and if I give this a run, it works. Of course it does. But now let's upgrade it to arrow functions. So if we switch back to rewrite this, yeah, this section here, you no longer have to add it. It'll be captured by value.
