Using callable Type0:00
You may be familiar with the callable pseudotype. So, for example, maybe you have some function where the user can pass in a closure that will be triggered by them, or a callback. Well, you can use the callable pseudotype. And basically what this does is it ensures that whatever you pass here must be something that can be triggered. And that means you can safely do something along the lines of this. Otherwise, if we were to, say, trigger and pass a string —let's try this out on just regular PHP 7.0— yes, a fatal error will be thrown.
Introducing iterable Type0:26
—let's try this out on just regular PHP 7.0— yes, a fatal error will be thrown. Or, of course, if we were just to give it a closure here and run it again, it's not going to fail. Okay, so nothing new there. But what is new is a similar pseudotype called iterable. For example, maybe you have a little helper function called dumpAll. And it's going to accept to start an array of items. And, yes, of course, you could just var_dump all of the items. But maybe you're going to format it.
And, yes, of course, you could just var_dump all of the items. But maybe you're going to format it. For whatever reason, you want to filter through each of the items and dump them individually for whatever reason. So we can say foreach items as item, var_dump the item, and then add, again, any other heading or formatting that you want. So that means if I were to say dumpAll, and then give it 1, 2, and 3, let's try it out. We run it. Yes, of course, everything works the way we would expect.
Limitations of array Hints1:17
let's try it out. We run it. Yes, of course, everything works the way we would expect. However, let's think about this array here. What we are saying here is we are mandating or we are requiring that what you provide to us is an array and nothing else. But think about it. Would you be that upset if the user gave us something that is iterable? So something that implements the Traversable interface in php, like IteratorAggregate or things like that. Would you be upset if they gave you that?
like iterator aggregate or things like that. Would you be upset if they gave you that? Everything would still work, but you've basically shut the door to that. Nope, you can only give us an array. I don't care if you give us something else that will still work. I'm not going to allow it. And, yeah, really pretty silly when you think about it. So instead, in PHP 7.1, you can use, if you want, and if you like type hints, you can use the iterable pseudotype. So now take a look.
Type-Hinting iterable2:08
and if you like type hints, you can use the iterable pseudotype. So now take a look. In PHP 7.0, it's going to fail with a type error. But if I switch over to our VM, it's using 7.2 dev, and if I run that, everything's going to work like normal. So that means we can now do something like this. Let's quickly create our own little Collection class. And what I'm going to do is it will accept an array of items, but I want this to be iterable. So an easy way to do that is to implement the IteratorAggregate.
Building an Iterable Collection2:34
but I want this to be iterable. So an easy way to do that is to implement the IteratorAggregate. I always spell that wrong. We can implement this interface, and all it's going to do is require us to add a getIterator method. So this says, what specifically are we iterating over? Well, it's going to be the items. So I'm going to return a new ArrayIterator, and then pass through our items. So now you can do stuff like this.
and then pass through our items. So now you can do stuff like this. We could say new Collection, and then, even by itself, you can use it with a foreach statement. Really cool. So let's give it a shot. I'm going to dump all and pass it a Collection now. And remember, before, we had an array type int, so it was going to fail. We run it, and it says, nope, we expected an array,
When to Use iterable3:36
And really, that's all there is to it. If you intend to loop over something, yes, you could accept array, or you could upgrade it to anything that can be iterated, or, you know what, just get rid of it entirely. It's up to you.
