PHP Generators

Posted on Posted in PHP, PHP, Tech

Ok, I have been reading Modern PHP by Josh Lockhart and I came across something that I never even knew that I could use to immediately update my code and that is generators.

If you don’t know what they are, they are basically one way iterators without all the fancy stuff. But the greatest use for them in my opinion is using PHP to process large files without having to load it all into memory. And in my case, we use web apps to process large CSV files with stack emissions monitoring data.

Previously, I was loading the entire CSV file via fopen and then reading via fgetcsv.

Now imagine doing the same thing but with generators, what would that look like?

function getRow() {
    $handle = fopen('fileName.csv', 'r');

    while(feof($handle) !== true) {
        yield fgetcsv($handle);
    }
}

foreach(getRow() as $line) { 
    // Process Line
}

So what is the benefit to all this? Memory management is the big one! It frees your server to perform other tasks, specifically the memory because only a line at a time is loaded into memory. The yield keyword is the key to generators.

There are other theoretical things that you can do with this functionality but the biggest benefit is processing large straight-forward datasets from outside resources.

I hope this small snippet of code will help someone like it helped me. Memory management is not something we should forget when implementing modern web applications.