Return Type Hints0:00
In the last lesson, you learned how to type in scalars, but we can also now type in returned values in PHP 7. For example, imagine you have a getUser function, and well, we expect a User instance to be returned. Well, how would we do it normally? We have class User here, and you would return a new User, or whatever, right? So if we were to var_dump getUser, and we run this, we get a User object. However, there's no protection against us just returning an array, or something like that. So if we run that, yeah, there's no protection there whatsoever. And again, you might be totally okay with this. If you really love the dynamic nature of PHP and Ruby and things like that, then you're fine. No issues there. However, if you want things to be just a little bit more strict, well, you can add a return type hint, like this. After the parentheses, add a colon, and then the type that you want to return. Whether that's a User
Enforcing Return Types0:49
be just a little bit more strict, well, you can add a return type hint, like this. After the parentheses, add a colon, and then the type that you want to return. Whether that's a User in this case, or an array, or a boolean, or any of these types that we've learned. In our case, we expect a User instance. Now, if we were to run this, yep, it's going to blow up. The return value of getUser must be an instance of User. Okay, so let's fix our error. We return a new User, in this case, and we should be good to go. Great. So once again, mostly, that's all there is to it. And what you may find is, this could be quite useful, especially for defining interfaces where other people will create the concrete implementations. You can just define the interface like so, SomeInterface, and then when you have your definition here for getUser, you can say, I expect that to return a User. Now, if we were to conform to that,
Interfaces and Runtime Checks1:37
interface like so, some interface, and then when you have your definition here for getUser, you can say, I expect that to return a User. Now, if we were to conform to that, class, some class, implements, some interface. Okay, now, if we were to just add getUser, like this, and return an array, well first, that's going to fail, where we are not honoring the contract. So yeah, we're back to the same thing as before. Now, quick note though, this will only be detected at runtime. So for example, yes, this is incorrect, but I've not instantiated and called this method. So if we run it, well, of course, we're not going to see anything, right? That's something to keep in mind. So let's do a new some class and get a User, and only now will it blow up. So we make it work, return new User, once again, create that class, and we should be good to go. Give it one more run, and yeah, everything's fine. Okay, so that
When to Use Them2:28
will it blow up. So we make it work, return new User, once again, create that class, and we should be good to go. Give it one more run, and yeah, everything's fine. Okay, so that basically does it. I would not say you need to specify return types for everything. If you prefer that, go ahead, but personally, I wouldn't be inclined to do that. I would only do this in situations where it's very important that the people who consume my interface adhere precisely to the contract.
