awesomite / iterators
Simulates yield in PHP < 5.5
Installs: 16 316
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires (Dev)
- awesomite/phpunit-4.8-fixer: ^1.0
- phpunit/phpunit: ^4.8.36
- symfony/process: ^2.8.16 || ^3.0
This package is auto-updated.
Last update: 2024-12-13 21:50:55 UTC
README
CallbackIterator
CallbackIterator
allows to simulate yield
feature from PHP 5.5.
PHP >= 5.5
<?php function getAllFromDatabase($tableName) { $page = 0; $perPage = 1000; while ($rows = Db::getRows($tableName, $page, $perPage)) { foreach ($rows as $row) { yield $row; } $page++; } }
PHP < 5.5
<?php use Awesomite\Iterators\CallbackIterator; function getAllFromDatabase($tableName) { $page = 0; $perPage = 1000; $rows = []; return new CallbackIterator(function () use (&$rows, $tableName, &$page, $perPage) { if (!$rows) { $rows = Db::getRows($tableName, $page, $perPage); $page++; } if ($rows) { return array_shift($rows); } CallbackIterator::stopIterate(); }); }
Versioning
The version numbers follow the Semantic Versioning 2.0.0 scheme.