Whats new in PHP 8.4
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
php84/LazyCollection.php

26 lines
541 B

<?php
require "libs/LazyCollection.php";
$lazyCollection = LazyCollection::make(function () {
try {
$handle = fopen('huge.txt', 'r');
if (!$handle) throw new Exception("Could not open file");
while (($line = fgets($handle)) !== false) {
yield $line;
}
} finally {
if (isset($handle) && is_resource($handle)) {
fclose($handle);
}
}
});
ob_start();
$lazyCollection->each(function ($line) {
echo $line;
ob_flush();
flush();
});
ob_end_flush();