PHPackages                             ehough/generators - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. ehough/generators

AbandonedArchivedLibrary

ehough/generators
=================

Simulate generators in PHP 5.3 and 5.4. Useful for backporting code.

v1.0.0(9y ago)19.2k—5%14MPL-2.0PHPPHP &gt;=5.3.29

Since Dec 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ehough/generators)[ Packagist](https://packagist.org/packages/ehough/generators)[ RSS](/packages/ehough-generators/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (1)Dependencies (1)Versions (3)Used By (4)

ehough/generators
=================

[](#ehoughgenerators)

[![Build Status](https://camo.githubusercontent.com/9d94ca6cac4afec40e1e45f7cb51a71d455a7ab3594fbbb6f18af6a5ebb49843/68747470733a2f2f7472617669732d63692e6f72672f65686f7567682f67656e657261746f72732e7376673f6272616e63683d646576656c6f70)](https://travis-ci.org/ehough/generators)[![Code Coverage](https://camo.githubusercontent.com/3124e81e73b70ca0a1cb0b2ede579542fdbcd71a188163accb84d3f377be27d3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f65686f7567682f67656e657261746f72732f6261646765732f636f7665726167652e706e673f623d646576656c6f70)](https://scrutinizer-ci.com/g/ehough/generators/?branch=develop)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/fb0e28506b280d9a7c20fa5ef0591682a07511ab79da0457b1e0ca085e965c8e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f65686f7567682f67656e657261746f72732f6261646765732f7175616c6974792d73636f72652e706e673f623d646576656c6f70)](https://scrutinizer-ci.com/g/ehough/generators/?branch=develop)[![Latest Stable Version](https://camo.githubusercontent.com/dec8cb0314b6f44a3c08503f3000fded73d72078a5fe1afbbfa72151a904281d/68747470733a2f2f706f7365722e707567782e6f72672f65686f7567682f67656e657261746f72732f762f737461626c65)](https://packagist.org/packages/ehough/generators)[![License](https://camo.githubusercontent.com/68df51311ad6348c5206b6af4c30f916f2914e39e72c1b813f0f721967606025/68747470733a2f2f706f7365722e707567782e6f72672f65686f7567682f67656e657261746f72732f6c6963656e7365)](https://packagist.org/packages/ehough/generators)

Easily backport [generators](http://php.net/manual/en/language.generators.overview.php) to PHP 5.3 and 5.4.

ABANDONED
=========

[](#abandoned)

The vast majority of PHP installations now natively support generators, so this library is obsolete.

Why?
----

[](#why)

This library makes it (relatively) easy to backport code that relies on generators for use on legacy (PHP &lt; 5.5) systems. If you don't need to backport code to PHP 5.3 or 5.4, you don't need this library.

Quick Start
-----------

[](#quick-start)

Say you need to use the following code on PHP 5.3:

```
$generator = function ($values) {
    print "Let's get started\n";
    foreach ($values as $key => $value) {
        yield $key => $value;
    }
    print "Nothing more to do\n";
};

$items = array('foo' => 'bar', 'some' => 'thing');

foreach ($generator($items) as $k => $v) {
    print "The generator gave us $k => $v\n";
}
```

The above code results in:

```
Let's get started
The generator gave us foo => bar
The generator gave us some => thing
Nothing more to do

```

Since the code above uses generators, it won't run on PHP 5.4 or lower. This library provides you with the `AbstractGenerator` class, which requires you to implement `resume($position)`. `$position` is incremented each time the generator resumes execution, and you can use the position to determine which part of the generator to run. So the above generator could be rewritten as:

```
use Hough\Generators\AbstractGenerator

class MyGenerator extends \Hough\Promise\AbstractGenerator
{
    private $keys;
    private $values;

    public function __construct(array $items)
    {
        $this->keys   = array_keys($items);
        $this->values = array_values($items);
    }

    protected function resume($position)
    {
        // first execution
        if ($position === 0) {
            print "Let's get started\n";
        }

        // still inside the for loop
        if ($position < count($this->values)) {

            // return an array of two items: the first is the yielded key, the second is the yielded value
            return array(
                $this->keys[$position],
                $this->values[$position]
            );
        }

        // we must be done with the for loop, so print our last statement and return null to signal we're done
        print "Nothing more to do\n";
        return null;
    }
}

$items = array('foo' => 'bar', 'some' => 'thing');
foreach (new MyGenerator($items) as $k => $v) {
    print "The generator gave us $k => $v\n";
}
```

The above code results in:

```
Let's get started
The generator gave us foo => bar
The generator gave us some => thing
Nothing more to do

```

The code is not nearly as clean and simple, but any generator can be rewritten using this library.

Yielding Keys and Values
------------------------

[](#yielding-keys-and-values)

You have three choices for what you return from `resume($position)`:

1. If you return null, you are signaling that there are no more statements inside the generator. The generator will be considered to be closed at this point.
2. If you return an array with two values, the first element is interpreted to be the yielded key and the second value is the yielded value.
3. If you return an array with one value, it is interpreted to be a yielded value, and `$position` will be used as the key.

Accessing Sent Values
---------------------

[](#accessing-sent-values)

You can access the last value sent in from the caller with `getLastValueSentIn()`. This might be `null`.

Handling Exceptions
-------------------

[](#handling-exceptions)

By default, if an exception is thrown into the generator (via `throw(\Exception $e)`) it will be rethrown back to the calling context. If you'd like to "catch" these exceptions, you can override `onExceptionThrownIn(\Exception $e)` and swallow or otherwise handle the exception.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3433d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/53b269d929f6ba572a15e4b069d9b59e0e99af74db1be60cbac7a56adfea9221?d=identicon)[ehough](/maintainers/ehough)

---

Top Contributors

[![ehough](https://avatars.githubusercontent.com/u/369261?v=4)](https://github.com/ehough "ehough (22 commits)")

---

Tags

generatorgenerators

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ehough-generators/health.svg)

```
[![Health](https://phpackages.com/badges/ehough-generators/health.svg)](https://phpackages.com/packages/ehough-generators)
```

###  Alternatives

[symfony/maker-bundle

Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code.

3.4k111.1M568](/packages/symfony-maker-bundle)[simplesoftwareio/simple-qrcode

Simple QrCode is a QR code generator made for Laravel.

2.9k27.6M92](/packages/simplesoftwareio-simple-qrcode)[crestapps/laravel-code-generator

An intelligent code generator for Laravel framework that will save you time! This awesome tool will help you generate resources like views, controllers, routes, migrations, languages and/or form-requests! It is extremely flexible and customizable to cover many on the use cases. It is shipped with cross-browsers compatible template, along with a client-side validation to modernize your application.

76591.7k1](/packages/crestapps-laravel-code-generator)[phpowermove/docblock

PHP Docblock parser and generator. An API to read and write Docblocks.

2524.0M4](/packages/phpowermove-docblock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
