Free Online Toolbox for developers

Exploring PHP Generators: Unleashing creative power with yield

Generators in PHP, introduced since PHP 5.5 (that’s a while ago…), leveraging the yield statement, are often underestimated. This article aims to unveil the creative power of yield and demonstrate how to use it to optimize the efficiency of your PHP programs. Generators go beyond mere value generation; they enable efficient iteration (A Generator is an Iterator). To grasp the workings of generators, let’s delve into their mechanism and explore the central role of yield.

Introduction++

yield in PHP is used in generators to produce values incrementally. It temporarily returns a value while preserving the state of the generator function between calls, optimizing memory and performance when handling large datasets.

Each invocation of yield returns a value while preserving the generator’s state between calls. The following example will output “1 2 3”:

function simpleGenerator() {
    yield 1;
    yield 2;
    yield 3;
}

$generator = simpleGenerator();

foreach ($generator as $value) {
    echo $value . ' ';
}

A generator can easily yield a key associated with a value using the double arrow:

function countryGenerator() {
    yield 'Germany' => 1;
    yield 'Belgium' => 2;
    yield 'France' => 3;
}

foreach (countryGenerator() as $countryName => $id) {
    ...
}

Efficient Handling of Large Data Sets

One major advantage of using yield lies in the management of large datasets. Instead of storing the entire set in memory, employ yield to process the data on the fly.

Asynchronous processing with yield:

function asyncDataProcessor($data) {
    foreach ($data as $item) {
        yield processAsync($item);
    }
}

Here, processAsync could represent a time-consuming operation, and the generator enables asynchronous processing without loading the entire dataset into memory.

Building Processing Pipelines

yield can be used to construct elegant processing pipelines by passing data from one generator to another.

Creating a processing pipeline:

function processPipeline($data) {
    yield from filterData($data);
    yield from transformData($data);
    yield from aggregateData($data);
}

In this example, each stage of the pipeline is a generator function, allowing a clean separation of responsibilities. However, be mindful of visibility…

Resource Management with yield

yield can also be employed to efficiently manage resources by automatically releasing those that are no longer needed.

Automatic resource release:

function resourceManager($resources) {
    foreach ($resources as $resource) {
        $data = loadData($resource);
        yield $data;
        releaseResource($resource);
    }
}

In this example, resources are automatically released once the associated data has been processed.

Retrieving the function’s return value

Since PHP 7.0, it is possible to further enhance generators by retrieving a value returned by the function at the end of its execution. Remember, the basic syntax means that when calling a generator, you obtain the generated iterator, not the return value.

Imagine we want to extend the generator that reads the contents of a file line by line, and additionally, we want it to count the number of empty lines in the file. How do we retrieve this count? To achieve this, we use the getReturn() method on the generated iterator:

function myGenerator() {
    yield 1;
    yield 2;
    yield 3;
    return 0;
}

$generator = myGenerator();
foreach ($generator as $data) {
    ...
}
// retrieve the result
$result = $generator->getReturn();

Bidirectional communication

It’s also possible to communicate in the other direction, from the iterator to the generator. Simply use the send() method of the iterator to send data back, which can be accessed in the generator by retrieving the “return value” of the yield:

function myGenerator() {
    $res = yield 1;
    if ($res === false)
       break;
    ...
}

$generator = myGenerator();
foreach ($generator as $data) {
    ...
    $generator->send(false); // Processing will stop
}

Conclusion

Generators are fantastic (short, but everything is said).




Leave a Reply