#php #phpvideotutorial #timestouch TimesTouch - PHP Programming Language - Iterables so for example maybe you have some function where the user can pass in a closure that will be triggered by them or callback well you can use the callable pseudo type 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 or of course if we were just to give it a closure here and run it again it's not gonna fail okay so nothing new there but what is new is a similar pseudo type called iterable for example maybe you have a little helper function called dump all and it's going to accept to start an array of items and yes of course you could just fart dump all of the items but 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 for each items as item evaru dump the item and then add yeah again any other heading or formatting that you want so that means if I were to say dump all and then give it one two and three 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 and PHP like iterator aggregates 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 ends you can use the iterable pseudo type so now and take a look in PHP 7.0 it's gonna 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 adorable so an easy way to do that is to implement the iterator aggregates I always felt that wrong we can implement this in our face and all it's going to do is require us to add a get iterator 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 array iterator 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 we're gonna dump all and pass it a collection now and remember before we had an array type n so it was gonna fail we run it and it says nope we expected an array but you gave us this object and even though you knew what you're doing I don't care I only work with arrays and no we're gonna change that to iterable come back and that's going to run just like before and really that's all there is to it if you intend to loop over or 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
Watch video PHP Programming Language - Iterables online without registration, duration hours minute second in high quality. This video was added by user 3D Animation Kids Story 14 November 2018, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,958 once and liked it 21 people.