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

Introducing Spaceship Operator0:00

Let's check out the combined comparison operator. Sounds kind of scary, looks like a spaceship, but in fact you actually call it the spaceship operator. And fun little fact, we call it the spaceship operator because the creator thought it looked like one of the Star Wars spaceships. And it does. Okay, so when would you use this? And it can be a little confusing at first. So let me do my best to explain it. First, we'll use an example. Imagine that we have an array of games. For example, Mass Effect, Super Mario Bros, Zelda, Fallout, and how about Metal Gear? Okay, so a simple array of games, and we want to sort them in alphabetical order. How do we do that in php? Very simply, we can just say sort($games).

Sorting Arrays Basics0:37

Okay, so a simple array of games, and we want to sort them in alphabetical order. How do we do that in php? Very simply, we can just say sort($games). Now, if I were to var_dump($games)... And by the way, nothing we see here is specific to PHP 7. This is standard stuff. But anyways, if we run this, sure enough, we can see the array sorted in alphabetical order. Now, alternatively, if we want to reverse sort this, well, we can use the rsort function. And now we'll get the same thing but in reverse order, starting with Zelda. Okay, very cool. But now, what if we need a little more control over how we perform this comparison? For example, maybe we want to sort them according to the number of characters per game. So if we're starting with the fewest characters—not characters in the game,

Custom Sorting with usort1:18

this comparison? For example, maybe we want to sort them according to the number of characters per game. So if we're starting with the fewest characters—not characters in the game, but characters in the word—then Zelda should be first and Super Mario Bros should be last. Okay. Well, we can use a usort function in php. Again, nothing new here. This has been available for quite a while. We will sort our games, but then use a callback here to define the comparison. Now, first, let's just reproduce what we had before, when we did this. How could we produce that? Well, we could use the spaceship operator. We could say return $a, and then create a spaceship and compare that against $b. Now, before I explain what that does, if we run it, it should be in alphabetical order, and it is.

Building User Collection Example6:14

And again, if we want to reverse it, notice all of the flexibility that we have here. Let's move on to one final example, and then we'll call it a day. Imagine that we have a collection of Users. So for example, a User has a name and also an age. And then why don't we fetch some getters here? All right. And I will clean this up. No need for doc box. We have a getter for the name and a getter for the User's age. Great.

We have a getter for the name and a getter for the user's age. Great. Next, maybe we have a collection of users. So I could call this userCollection. And that will be instantiated with an array of users that we will assign, like so. Now, imagine that you want to sort this collection in some special way. For example, maybe you want to sort the users according to the user's name, or the user's age, or something different altogether. OK. Well, maybe you could add a sortBy method, and we will reference the column, so to speak.

Implementing sortBy with Spaceship7:10

OK. Well, maybe you could add a sort by method, and we will reference the column, so to speak. All right. Well, now we know exactly how to do that. We can use the spaceship operator. We'll say usort($users, and for our callback, we could say, like, $user1 and $user2, if that helps. And now we can just call the necessary method. So we accept the column or property, if you want. And then just say return $user1 and fetch that property through its getter.

So we accept the column or property, if you want. And then just say return user1 and fetch that property through its getter. And then we can use the spaceship operator to compare that against user2's property or method. OK. Let's try this out. So we will create a collection and new that up. And that will be composed with an array of users. New User, Jeff, is 30. New User, Taylor, is 29, unfortunately. And then new User, Jane, is 50.

New user, Taylor, is 29, unfortunately. And then new user, Jane, is 50. And new user, Susie, is 10. And now let's say collection sortBy. And why don't we start with name? And finally, var_dump collection. And why don't we grab the users here? Like so. Make sense? OK.

Make sense? OK. New up a collection, give it some users, and then specify that we want to sort our collection of users according to the name. And remember, usort is basically destructive. So it's not like you have to return anything. However, if you want, you could return the collection object. You could return the users directly here, if you want. Whatever you want to do. Anyways, if we run this, and let's take a look.

Testing Sort by Name/Age8:50

Whatever you want to do. Anyways, if we run this, and let's take a look. We're sorting by the name. So we get Jane, Jeff, Susie, and Taylor. That's correct. Why don't we now sort according to the age? OK. Susie should be on top now. And she is, finally, with Jane, who is 50, at the bottom. And by the way, perhaps we should call that method.

This has been around in some languages for a while. So it's nice that we finally have it in php.

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