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

Introducing Union Types0:00

Next up we have union types. This is really simple. Think of it as a way to say, I expect this type or that type. In older versions of PHP, this wasn't so easy. I'll give you a couple examples. Maybe a User can befriend another User. Or that's too formal. How about elementary school? Make friends with another User. You get the idea. So if you have Joe and then you have Sam, they can make friends by saying, Joe, make friends with Sam. And then up here, maybe var_dump. Yay, friends. And if we run this, even in older versions of PHP, of course that's going to work. However, maybe you decide it's acceptable for this to be null. Maybe if there is no User, they receive an invitation email to sign up for the site. Something like that. You get the idea. Well, in PHP 7, there actually is a form of a union type. And that would be the question mark before the type. This is effectively

Nullable Types in PHP 70:49

the site. Something like that. You get the idea. Well, in php 7, there actually is a form of a union type. And that would be the question mark before the type. This is effectively a union type. We're now saying we expect User or null. And I always think of it like a question. User? Like maybe a User or maybe not. Maybe null. But yeah, that would be an option. So if we gave this a run, that would work. But if we gave it something that was null where we set the default up here, it's not going to blow up the system. And again, this all works in php 7.4. I'm just showing you there was a form of a union type. But now it has been expanded. So if we were to rewrite this, and by the way, I would keep this. If all you're accepting is User or null, don't change anything. But it's effectively this. Give it a run in php 7 and it blows up. Unexpected pipe. But if I run it with php 8, it works. And do note, you can expand this as much

PHP 8 Pipe Unions1:37

don't change anything. But it's effectively this. Give it a run in php 7 and it blows up. Unexpected pipe. But if I run it with php 8, it works. And do note, you can expand this as much as you want. So maybe you're going to accept a User or a string to represent the email address. And then you check, well, do we have a User in our system? Then update a pivot table to make them friends. Otherwise, dispatch an email to this address to invite them to your site. So now, if I were to say sam@example.com, once again, in php 8, that's going to work. In php 7, it will not. So again, in past versions of php, what you would often do is maybe not use any types at all. Instead, you might refer to doc types. Here, you could say, I expect a User or string. But yeah, if you like types, you don't have to do that anymore. You can add it directly here. And then, if you want, even get rid of the doc block. So maybe how about one more example? Maybe a User

Cancel API Example2:31

if you like types, you don't have to do that anymore. You can add it directly here. And then, if you want, even get rid of the doc block. So maybe how about one more example? Maybe a User can cancel their subscription. And we're going to accept an argument for whether it should be canceled immediately. So by default, that's set to false, which means if you call cancel, it's going to cancel their subscription at the end of their billing period, which is pretty common. So if we have our User, Joe, and by the way, this can be typed. Joe could then call cancel, and any of these forms would work. false. And in fact, we could just have one where we don't pass anything at all. And you'll see here, PHPStorm is letting me know, hey, that's redundant. You don't have to include that if you don't want to. But yeah, it supports this at the moment. But again, maybe you want to extend the API here to support providing a timestamp. So I don't want to cancel them.

include that if you don't want to. But yeah, it supports this at the moment. But again, maybe you want to extend the API here to support providing a timestamp. So I don't want to cancel them immediately, and I don't want to cancel them at the end of their period. I want to cancel them next week. And that could be a DateTime instance. If you're using Carbon, you could do something like this. Maybe you want to support all of these. The only problem is, if I var_dump immediate, which is probably no longer the right parameter name. But yeah, if I give that a run, call to undefined. Oh yeah, of course. I don't have Carbon here, so I'll just simulate that. But any kind of DateTime instance. Of course, if I were to run that now, well think real quick, will that be accepted? Or will it blow up? I mean, give it a run. It's accepted because we don't have strict types turned on. So think about it. In this case, we said we expected a boolean,

Strict Types Behavior4:04

will that be accepted? Or will it blow up? I mean, give it a run. It's accepted because we don't have strict types turned on. So think about it. In this case, we said we expected a Boolean, but we gave it a string. So php goes, okay, let's just parse that string into a Boolean. So for example, if I were to open the shell, and I turn that into a Boolean, of course that's going to evaluate to truthy. And I'm sorry, var_dump. Try it again. There we go. Of course a string with characters will evaluate to true, and an empty string would evaluate to false. So that's all that's happening here. When we pass a string, it gets turned into a Boolean. So what you can do, by the way, is you can declare strict types to one. Now we're effectively saying, hey, when I said I wanted a Boolean, I meant the Boolean. I'm not going to do any work for you here. And in fact, you'll see PHPStorm will pick that up for us and let you know this method expected a Boolean,

Union Fix for Strict Types4:56

said I wanted a boolean, I meant the boolean. I'm not going to do any work for you here. And in fact, you'll see PHPStorm will pick that up for us and let you know this method expected a boolean, but you passed a string, and that's not going to work. So if we were to give it another shot, now it blows up. Must be a type bool. But yeah, again, if you did not have that there, it would convert it to a boolean. Something to consider. Anyways, if we want this to work, we need a union type. So we're going to say I expect a boolean or, and again in real life, you'd probably say something like a Datetime instance. And that way, if you're passing a Carbon instance, I'm pretty sure that extends Datetime, doesn't it? So you could just use the top level there. But to keep it simple for the example, we'll just turn it back to a string, and then you would handle it there. So now it's no longer going to blow up, except if you were to run it in PHP 7 or below. And that's

that might be a signal to add a couple more helper methods.

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