Practical usage of PHP 5.5 generators: yield keyword

With the release of PHP 5.5 there are several new language features but one of the most interesting is the new yield keyword.

The PHP manual states

“Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.”

The key to understating the way the yield works verses a normal function is that rather than generating all of your data and returning the final array when it’s done; you yield the value as it’s generated. The state of the generator function is saved after you yield and then its state is restored when called again so the iteration can continue.

The most practical use case of generators I have found thus far is for skating around memory usage issues. If you’ve got a function that generates a large array that you need to iterate over via foreach generators are your ticket.

About these ads

23 thoughts on “Practical usage of PHP 5.5 generators: yield keyword

  1. Awesome. I was recently brushing up on Python generators. Good to know they’re in the PHP toolkit, too!

  2. I am looking forward to use yields, but i wonder if there will be any good use for them. I can imagine that yield will be greate way how to work with objects where value can be returned through mutiple calls without a need to carry whole chunk of data between objects.

    But to this day only example i saw was the range example and file read example. I hope to see something interesting like iterating through entities returned from database.

    • Suppose you want to iterate through a result set from a database but also do some PHP-side filtering (for example something that’s not easily done through the database query directly).

      Without generators, you have to pass off the result set as it is and let the calling function dealing with the filtering, or you have to iterate over the entire result set and store the filtered results in an array. Neither is an ideal situation.

      The best solution would be to keep the result set inside the calling function and only yield the filtered results to the caller.

  3. Oh my goodness! Impressive article dude! Thank you, However I am going through issues with your
    RSS. I don’t know the reason why I can’t join it.

    Is there anybody else getting similar RSS problems? Anybody who knows the answer will you kindly respond?
    Thanx!!

  4. At this time it sounds like BlogEngine is the preferred blogging platform available right now.
    (from what I’ve read) Is that what you’re using on your blog?

  5. Pingback: Entendendo Generators, Iterators e Yield | EvAnDrO vEnAnCiO

  6. Wonderful beat ! I wish to apprentice even as you amend your site, how could i subscribe for a blog site?
    The account helped me a applicable deal. I have been a little bit acquainted of this your
    broadcast provided brilliant clear concept

  7. Definitely believe that which you stated. Your favorite reason seemed
    to be on the internet the easiest thing to be aware of.
    I say to you, I certainly get annoyed while people think about worries that they just don’t know
    about. You managed to hit the nail upon the top and defined
    out the whole thing without having side-effects , people can take a signal.
    Will likely be back to get more. Thanks

  8. You can choose to possess the proceeds of your respective unsecured
    loan deposited directly to your checking account or piggy
    bank via electronic funds transfer EFT within just hours
    after being approved Unknown if you sign up for an internet loan, you’ll be able
    to rest assured that the knowledge that you’ve given is going to be kept confidential and safe.

  9. Hi, I do believe this is an excellent blog. I stumbledupon it ;) I’m going to revisit once
    again since I bookmarked it. Money and freedom is the
    best way to change, may you be rich and continue to
    guide other people.

  10. I see a lot of interesting articles on your page. You have to
    spend a lot of time writing, i know how to save you a lot of time, there is
    a tool that creates unique, SEO friendly posts in couple of seconds, just type in google – k2 unlimited content

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s