Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

2.2 Return Type Declarations

PHP 7 gives us the ability to add a type hint for the return data type of a function. You'll learn how to use return type declarations in this lesson.

2.2 Return Type Declarations

PHP7 introduces, what are called return type declarations, but you can think of them as type answer for what a function returns. And by saying that, you probably already understand the concept behind them. So, let's write a function called sum and we will accept an int called A, let's also have an int called B. And we will simply return $a + $b. Now we are working with integer values our function name is sum so we could infer that this function returns an integer value. I mean that's a reasonable thing to assume. However we could be specific and say that this function returns an integer by using a return type declaration. To do that, we use a colon after the parameter list and then the return type. In this case is going to be an int and we can use all of the types that we could use for type hints, so that means arrays classes and scalar types as well. And the same rules that apply to type hints also applies to type declarations in that the return value has to be use as our return type. So it's going to coerce anything that can be coerced into an hint in this case. So if we are returning a string of one then, it is going to coerce that string into an int. And we can see that by simply saying var_dump, we'll pass in our result here, and in the command line, we will run this, and there we have an int with a value of 1. So inside of the function, the return statement doesn't have to actually return the type but that value has to be coerced into that type. So now the question that's probably plaguing your mind is, why should you use a return to type declaration? Well, for one and ensures that your functions return the type of data that you expect them to, it's a run time type check if you will. Unless if you have an editor that's going to perform these types of checks during development. It's also extremely useful inside of an interface, like for example let's say that we have a class called user, and our particular application has an interface called I membership. So that we can plug in whatever membership system that we need for our application. So let's say that our data store is a my single database. So we'll have a my sequel membership that implements I IMembership, so that if for some reason we decided to change our data store to a Microsoft SQL database or Oracle, or something else we could just write another membership class that implements IMembership. We could plug that into our application and then we are good to go, so one of the methods that this membership class needs would be a method to get a users. So we would have a public function we'll call this get user. We will excepts probably an e-mail address and before we had return type declarations that's exactly what we would have so let's go ahead and implement this. Inside of r, my sequel membership class, so we have our email and in this case we would simply return a new user. But now that we do have return type declarations, inside of our interface we can say that our get user method has to return a user. So that if we returned anything other than a user inside of the concrete membership class we would get an error. In fact let's see what that does, so let's creates our membership system. We'll just new up my SQL membership and then let's call sysgetuser, we'll pass in some string value. This is of course a fictional email address. Let's hop on over to the command line and whenever we run this. We get an error declaration of my sickle membership get user must be compatible with, I membership get users. So this is the first thing because we said that the get user method has to return a user. We also have to specify that inside of our concrete class and in doing so We have declared that the get user method is returning the user. So if we return anything other than a user, then we are going to get another error. So if we simply return an integer, we run this we see that the return value of get user must be an instance of user. So we are returning to user, we run our code and everything is fine, now, we don't have to return a simple user. We could have my SQL user, because we have a membership system using a My SQL database. It kind of makes sense to have a specific user class for that membership system and we can say that this extends user. So that now my sequel membership class won't return just a user it will return a my sequel user and this is perfectly fine because a my sequel user is still a user. So when it comes to using return type declarations, I have a feeling that the usage for are some function is going to be used by some developers, because they might like that particular type of type checking. And it certainly has its usage, but it really shines here, by using a return type declaration inside of an interface. We have stated that any class that implements this interface has to adhere to everything including the return type declaration.

Back to the top