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

Modeling User Profile Access0:00

Let's begin with the null-safe operator. So imagine you have a User, and a User has a Profile. Now in real life, this would often be an SQL query. Give me the Profile with the given userId. But in our case, I'm just going to hard-code it. Alright, so now, if we set up our Profile class, maybe every Profile has a location or employment. And I'll say Web Developer. Alright so sure enough, if I had a User, and I wanted to grab their employment, I would say user, give me your profile, and give me your employment, and we'll echo that out,

Alright so sure enough, if I had a User, and I wanted to grab their employment, I would say user, give me your profile, and give me your employment, and we'll echo that out, and give it a run. And sure enough, we get Web Developer. But that's going to work in php 7 as well. Of course, nothing new here. However, what you'll find is sometimes, especially when this might be a database query, there could be instances where there is no profile for that User. So that method returns null. And this can often be a trap that you run into.

Handling Null Profile Errors1:01

So that method returns null. And this can often be a trap that you run into. You assume there's a profile, even though there isn't. So in that case, you give it a run, and everything blows up. Call to a member function employment on null. Of course, profile returns null, and now you're trying to do effectively this, and it doesn't work. Alright, so what do we do in these cases? Traditionally, you would say something like, well, get their profile, and save it to a variable.

Traditional Null Checks1:23

Traditionally, you would say something like, well, get their profile, and save it to a variable. And if they have a profile, only on that condition should we proceed and grab their employment, and echo that out. So in this example, we're not going to see anything at all, because there is no profile. But if there was, give it another run. That would work. But yeah, it's kind of messy. I'm sure you've done something very similar to this. But in php8, it's a little bit cleaner.

Using Null-Safe Operator1:47

I'm sure you've done something very similar to this. But in php8, it's a little bit cleaner. We can change this to user, profile, ?. This is the new operator, the null safe operator, and then employment. And we'll echo that out. Okay, so now, if I give it a run, we get web developer. But on the condition that this returns null, it doesn't blow up. It instead returns null. And in fact, if we were to var_dump that, you would see it. Sure enough.

And in fact, if we were to var_dump that, you would see it. Sure enough. However, if I ran it with php7, it does blow up. Of course. It doesn't know what that operator is. Now you can apply this anywhere. So for example, you could do this. If you continued chaining, be careful of law of the meter, but it would still work. So in this case, it would read, if we have a User, and if that User has a Profile, then give me their employment.

Defaulting with Null Coalescing2:36

So in this case, it would read, if we have a User, and if that User has a Profile, then give me their employment. And again, we would get null. So what we could also do is something like, give me the User's Profile, if they have one, give me their employment, otherwise, we'll go back to the null coalescing operator, and we'll default to not provided. Give that a run, and we get not provided, but assuming they do have a Profile, of course, we would get web developer. Big fan of little improvements like this.

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