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

Evert Pot:
Save memory by switching to generators
Aug 11, 2015 @ 09:45:51

Evert Pot has a post to his site showing you to conserve memory with generators in your PHP scripts. Generators are a language feature that allows you to generate/manipulate data like an iterator without needing to pre-generate the array beforehand.

Since the release of PHP 5.5, we now have access to generators. Generators are a pretty cool language feature, and can allow you to save quite a bit of memory if they are used in the right places. [...] It's not uncommon in complex applications for the result of a function like our [example] to be passed to multiple functions that mangle or modify the data further. Each of these functions tend to have a (foreach) loop and will grow in memory usage as the amount of data goes up.

He uses a common example of fetching a set of articles from a database to show how memory consumption could get huge when a large number of articles are involved. He rewrites the example using generators instead, making use of the yield functionality to only fetch one record at a time and map it to the object structure. He also includes a few things to watch out for when using generators including the different return value of the method (iterator, not an array). He also points out an issue where the array_* functions will no work on iterators so you'd need to convert it back to an array before use.

tagged: memory generator switch example records yield

Link: http://evertpot.com/switching-to-generators/

How to use the “yield” keyword in PHP 5.5 and up
May 23, 2014 @ 12:09:47

In a recent post to the LeaseWebLabs blog Maurits van der Schee looks at the use of the "yield" keyword in PHP 5.5 to work with generators. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values but it yields values one at a time.

The concept of generators is not new. The “yield” keyword exists in other programming languages as well. As far as I know C#, Ruby, Python, and JavaScript have this keyword. The first usage that comes to mind for me is when I want to read a big text file line-by-line (for instance a log file). Instead of reading the whole text file into RAM you can use an iterator and still have a simple program flow containing a “foreach” loop that iterates over all the lines.

He includes a few code examples showing a class that can read in data from a file in chunks and output the lines as they're extracted (versus using something like file). He also talks about a small performance comparison in working with the file pointer, fread over fgets. He even makes a simple benchmark script to compare the overall time and memory consumption of the fetching of different byte "chunks" from the file.

tagged: yield generator file read fread fgets memory time benchmark

Link: http://www.leaseweblabs.com/2014/05/how-to-use-yield-keyword-php

Generators in PHP
Aug 06, 2013 @ 12:25:50

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.

tagged: generator tutorial introduction example yield inject keys

Link: http://phpmaster.com/generators-in-php

Blake Gardner:
Practical usage of PHP 5.5 generators: yield keyword
Jun 24, 2013 @ 11:54:42

With the release of PHP 5.5 came a whole group of new features, including the "yield" keyword for better handling of values in iteration. Blake Gardner has posted a practical example of its use to his site today.

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.

He shows a basic use of "yield" in a simple foreach of 1000000 values. In the first example, memory is exhausted and the second yields the values as they come, reducing the overhead significantly. The "range_yield" function returns them as the "for" loop generates them.

tagged: yield feature practical use tutorial generator

Link: http://blakegardner.co/2013/06/24/practical-usage-of-php-5-5-generators-yield-keyword