Simple iterators Use generators PHP 5.5


show more Simple iterators using generators provides you with in-depth training on Developer. Taught by Jon Peck as part of the PHP 5.5 First Look show less
please wait ...

Simple iterators using generators

One of the most common programming needs, is to be able to perform an action on every element of an object. For example, using a foreach on an array, to get each key in value, or a foreach on a standard object, to get each parameter name and value. The mechanism for implementing this behavior is known as an iterator, which is used for traversing through all elements of an object or an array. Iterators can be explicitly defined for custom classes, but that can be an unwieldy and complicated process involving implements and, more than a handful of functions.

What if I want to act on a subset of an array, and perform some logic as I go along, but still be able to iterate through the object? Well, PHP 5.5 has introduced generators, which are an easy oilerplate free way, of implementing an iterator by using the special yield keyword. Yield provides a value to codes looping over the generator, and pauses the generator function execution. It'll look very similar to return sta...

Simple iterators using generators
Video duration: 2m 45s 42m 29s Intermediate

Viewers:

Simple iterators using generators provides you with in-depth training on Developer. Taught by Jon Peck as part of the PHP 5.5 First Look

Subject:
Developer
Software:
PHP
Author:
please wait ...