Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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 statement. I'm going to use a generator to draw cards from a deck. I'm going to switch the IDE, navigate to Classes and open Game.php.
I'm going to code a public function draw cards. This function takes one argument, which is the number of cards to draw. I'm going to start with some simple logic. So if, number of cards not equals intval number of cards, because you can only have whole cards, throw new Exception, cannot take partial cards. Now that I have that out of the way, I am going to use the number of cards as part of a for loop. For count equals 0, count is less then the number of cards, and count ++. I'm going to get the cards from the deck, so cards equal this. Deck, get cards.
If there are any, recycle the deck, meaning put the discard in the deck and shuffle. So if, empty, cards, this, recycle. At the end of the loop, I'm going to yield a drawn card. This means each time it goes through the loop, it'll generate a drawn card. Yield, this_deck, draw. Save and navigate to the construct above. I'm doing a foreach for the number of players and cards per player, and putting each card yielded by the generator in the hand.
To see this in action, switch to the browser and click on Game. The deck, discard, and two players are rendered with their drawn cards.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99529 Viewers
56 Video lessons · 112754 Viewers
71 Video lessons · 81567 Viewers
131 Video lessons · 39155 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.