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

Torben Köhn:
PHP Generators – A Guide and Tutorial
Jun 22, 2016 @ 18:45:44

For those that may have heard about generators but aren't too familiar with them or what they do, Torben Köhn has posted a great introduction to them and their functionality.

In my in-depth guide about iterators I talked about what iterators are exactly and how you can use them. At the end I told you that I’ll also write one for generators. Here it is.

First off, if you don’t know what an iterator is and you’d not be able to explain to someone else what it is, you will not have much fun with this because you won’t exactly recognize the use-cases. I suggest you read my iterator-guide first. After this, don’t get scared off by some confusing words used here, I’ll try to clarify every single one.

He breaks up the rest of the post into different sections, each walking you through different aspects of generators:

  • What is a generator?
  • The yield-keyword
  • Iterating a generator
  • Yielding keys
  • Yielding in a loop
  • An infinite generator

He wraps up the post sharing some real use-cases for generators to help you understand them with a bit more practical application (including stacking them, file system handling and co-routines).

tagged: generators tutorial introduction guide beginner

Link: http://tk.talesoft.io/2016/06/06/php-generators-a-guide-and-tutorial/

The PHP.cc:
PHP 5.5: Generators
Jul 10, 2013 @ 16:49:04

In this latest post to The PHP.cc's blog, Sebastian Bergmann talks about using a new feature in PHP - generators.

A generator is an interruptible function that returns a sequence of values (using the yield keyword) instead of a single value (using the return keyword). Two things happen when the yield statement of a generator function is executed: the argument of the yield statement is yielded and the execution of the generator function is suspended. The execution of the generator function is resumed when the next value is requested.

He starts with a simple example, showing a basic foreach loop calling a generator to produce (yield) an incrementing number each time. He also provides a more "real world" use case - using generators as data providers for PHPUnit tests. His example generates a new "Address" object each time the provider is called with a bit of "randomized" information included.

tagged: generators introduction phpunit data provider tutorial

Link: http://thephp.cc/viewpoints/blog/2013/07/php-5-5-generators

Aaron McGowan:
Finally generators exist as of PHP 5.5
Jan 17, 2013 @ 18:37:27

In this new post to his site Aaron McGowan talks about new features of the upcoming PHP 5.5 release - the "finally" keyword and generators.

PHP 5.5 has recently been released as an ALPHA release, meaning there are still bugs, code is being tested and features being added. With the 5.5 release, many of us PHP developers have a few wonderful new features that we should be taking advantage of almost immediately.

He gives brief introductions to these two new features, including some code examples (but getting a bit more into the generators side of things). You can find out more about these two features and how to implement them when PHP 5.5 comes around from the PHP sitel: generators and finally (actually from the PHP wiki).

tagged: finally generators introduction version update

Link:


Trending Topics: