News Feed
Jobs Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Anthony Ferrara:
What Generators Can Do For You
July 24, 2012 @ 08:43:58

Anthony Ferarra has a new post looking at using generators in your code (as recently proposed for addition in PHP's core (Possibly for 5.5.0). While I believe that this is a great tool, it appears that many PHP developers aren't familiar with the concept of generators. So I thought I would take a little time and explain some of how it works, and how it can be used to greatly simplify code.

He explains the concept of "generators" as an easier way to implement iterators. In his example he shows how to refactor is file handling iterator to replace it with generator functionality. It uses a new keyword, "yield", to return a Generator instance that can then can be used much like the file iterator without the need for all of the code to create the iterator itself. His more complex example shows how to replace an ArrayObject instance by a little trick inside its "getIterator" method.

0 comments voice your opinion now!
generator proposal rfc tutorial iterator arrayobject


blog comments powered by Disqus

Similar Posts

ServerGrove Blog: Symfony2 components overview: Templating

Deepak Radhakrishnan\'s Blog: Configuring Apache - PHP - MySQL on Windows (WAMP Configuration)

Chris Jones' Blog: Speeding up DBMS_OUTPUT in PHP OCI8

Max Horvath's Blog: How to enable the Xdebug debugger in Zend Studio for Eclipse

ITNewb.com: PHP Encryption / Decryption Using the MCrypt Library (libmcrypt)


Community Events











Don't see your event here?
Let us know!


application security component code hack performance framework introduction composer symfony2 package podcast opinion series unittest hhvm release language facebook install

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework