Introducing Scalar Type Hints0:00
New in PHP 7 is the ability to type hint scalar types. And if you're not familiar with that term, think of it as just the most basic primitive type, things like int, or string, or bool. Now you probably know, for example, you could already type hint more complex things, like array or class. So this has been doable for quite some time. But now, with version 7, like I said, we can type hint scalars. So imagine instead this was like a setter for an age. Well, before you would just set the age and assign it. Or now, if you prefer, and remember, if you don't like this idea, you're not a fan of type hints, and you like things to be as dynamic as possible, you don't have to use any of this. It's all opt-in. Okay, so I could now say int if I want. Let's see this in action. I will call setAge and pass through some string. And let's see what we get. Now we can test this out in two ways. One would be to go to this URL
Testing Type Enforcement0:48
int if I want. Let's see this in action. I will call setAge and pass through some string. And let's see what we get. Now we can test this out in two ways. One would be to go to this URL that we set up in the previous video and give it a run. And we see argument one passed to setAge must be an integer, but you gave me a string. So now I will throw a fatal error. Now alternatively, you could just run this from the command line. We're in that source directory. So I should be able to say php, and don't forget that's set to v7. So I can say php on that file, and we'll get the same output if you prefer. Okay, so let's make it pass. We'll change this to $myAge, 30. And if we run it again, all is well. Now one quick note on this, though, this is not entirely strict. So for example, if you were to pass something like 30 as a string, well, php is going to do its best to translate that into whatever type you specified. So in this case, it should turn it into
Type Coercion Examples1:38
So for example, if you were to pass something like 30 as a string, well, php is going to do its best to translate that into whatever type you specified. So in this case, it should turn it into the number 30. Let's try it out. We run it, and there you go. It is an integer. But again, if you pass something that can't be translated to a number, of course it's going to blow up. Another example would be like for booleans. So maybe setIsValid or something like that. And this would accept a boolean. We'll call that valid, and then again on the fictional object, you would assign it or something like that. Let's just var_dump it in this case, and then we'll say setIsValid. Well, yeah, we could do true. And if we run that, we get true. But if I also passed, for example, 1, then we should get true as well. It just gets coerced. 0 would be false. −1 would be true, right? That makes sense. A general string will be translated into the boolean. However, if
Wrapping Up Scalar Types3:18
that's all there is to it when it comes to scalar types. You don't have to use them if you're not a fan of them. But if you are, these will be a welcomed addition.
