On PHPMaster.com a tutorial has been posted talking about one of the newer features in PHP - generators. In the tutorial Stefan Froelich walks you through how they work and a few examples of their use.
Generators in PHP
If you've followed my previous posts about iterators then you'll know that iteration is an important programming concept, but implementing the required interfaces to create an iterable object can be a hassle at best because of the amount of boilerplate code that is required. With the release of PHP 5.5, we finally have generators!
He starts with a more practical example - pulling lines from a file, one at a time, without the overhead of having to read in the entire file at once. He also includes an example of returning the keys from the generator (not just the value) and injecting values with the "send" method.
Link: http://phpmaster.com/generators-in-php
没有评论:
发表评论