PHPackages                             jgswift/delegatr - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. jgswift/delegatr

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

jgswift/delegatr
================

PHP 5.5+ delegate system

0.1.6(11y ago)31611MITPHPPHP &gt;=5.5

Since Apr 22Pushed 11y ago1 watchersCompare

[ Source](https://github.com/jgswift/delegatr)[ Packagist](https://packagist.org/packages/jgswift/delegatr)[ RSS](/packages/jgswift-delegatr/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (6)Dependencies (5)Versions (6)Used By (1)

delegatr
========

[](#delegatr)

PHP 5.5+ delegate system

[![Build Status](https://camo.githubusercontent.com/8a06cb87da7a1ff5b68c025fa89156987dc535551e06bf74c41b0eb0b7fcbd76/68747470733a2f2f7472617669732d63692e6f72672f6a6773776966742f64656c65676174722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/jgswift/delegatr)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/670b3424b0d79f4cb5cf3125d9b961c59090f288e1b1aa649d90f8e30ff937ff/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a6773776966742f64656c65676174722f6261646765732f7175616c6974792d73636f72652e706e673f733d35643662396439313634303235393734353938383638643039383432333139323536666333316265)](https://scrutinizer-ci.com/g/jgswift/delegatr/)[![Latest Stable Version](https://camo.githubusercontent.com/0ad6ca108d709c8df8861947295525b08b89c63281093a14983e8d26e4e13127/68747470733a2f2f706f7365722e707567782e6f72672f6a6773776966742f64656c65676174722f762f737461626c652e737667)](https://packagist.org/packages/jgswift/delegatr)[![License](https://camo.githubusercontent.com/3b9ee26a334e8297a554f9d0f011a225029191496e15983c9cc3a5d0ec825853/68747470733a2f2f706f7365722e707567782e6f72672f6a6773776966742f64656c65676174722f6c6963656e73652e737667)](https://packagist.org/packages/jgswift/delegatr)[![Coverage Status](https://camo.githubusercontent.com/ee11fc4e95af68ff9c1ebf75f156aaf5c9d520ac9fd25633532d580d8baef306/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6a6773776966742f64656c65676174722f62616467652e706e673f6272616e63683d6d6173746572)](https://coveralls.io/r/jgswift/delegatr?branch=master)

Installation
------------

[](#installation)

Install via cli using [composer](https://getcomposer.org/):

```
php composer.phar require jgswift/delegatr:0.1.*
```

Install via composer.json using [composer](https://getcomposer.org/):

```
{
    "require": {
        "jgswift/delegatr": "0.1.*"
    }
}
```

Dependency
----------

[](#dependency)

- php 5.5+
- [jgswift/FunctionParser](http://github.com/jgswift/FunctionParser) - forked from [jeremeamia/FunctionParser](http://github.com/jeremeamia/FunctionParser)
- [adlawson/veval.php](http://github.com/adlawson/veval.php) - eval fallback using virtual file system

Usage
-----

[](#usage)

### Built-in Lambda (with Serializable)

[](#built-in-lambda-with-serializable)

One of the cumbersome aspects of closures in php is the need to explicitly define the context variables with the "use" statement. Delegates can make that process less painful by allowing you to specify an associative array to dynamically define context.

```
$x = 10;
$y = 2;

$lambda = new delegatr\Lambda(function() {
    return $x + $y;
}, get_defined_vars());

var_dump($lambda()); // 12
```

`get_defined_vars` returns a (non-referenced) list of variables defined in the same scope `get_defined_vars`is called. Thus `$x` and `$y` are dynamically added the the closure's "use" statement and inserted into the closure scope at run-time.

### Writing Custom Serializable Closures (using trait)

[](#writing-custom-serializable-closures-using-trait)

Delegatr doesn't just wrap closures in an object, it uses eval to enable the serialization of closures - something native php can not do.

The following class uses the Serializable delegate to accomplish this.

```
class MyDelegate {
    use delegatr\Serializable;
}

$delegate = new MyDelegate(function() {
    return 'foo';
});

$serial_string = serialize($delegate);

$delegate2 = unserialize($serial_string)

var_dump($delegate2()); // returns 'foo';
```

Using the above trait prevents delegatr from imposing on your domain but if that isn't a concern just instantiate or inherit the Lambda class as shown below.

```
$delegate = new delegatr\Lambda(function() {
    return 'foo';
});

class MyLambda extends delegatr\Lambda {
    /* ... */
}
```

### Simple delegate (without serialization)

[](#simple-delegate-without-serialization)

A simpler implementation is also available without \\Serializable included

```
class MyDelegate {
    use delegatr\Delegate;
}

$delegate = new MyDelegate(function() {
    return 'foo';
});

var_dump($delegate()); // returns 'foo';
```

### Eval Fallback &amp; Vulnerability

[](#eval-fallback--vulnerability)

The [eval](http://php.net/manual/en/function.eval.php) function is heavily relied on in this package.
If you are not comfortable with eval or do not understand the security risks, I do not suggest you use this package.

However, *eval* itself is *not* required for this package.
Delegatr uses [adlawson/veval.php](http://github.com/adlawson/veval.php) to compile scripts at run-time even in environments where *eval* is disabled. Bypassing eval in this way doesn't reduce the risk of code injection.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity52

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

Every ~30 days

Recently: every ~3 days

Total

6

Last Release

4249d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e60e96f18d8a9dc2aff1363eedbaf210acc96666e7497d2a83d5a416ef397295?d=identicon)[jgswift](/maintainers/jgswift)

---

Top Contributors

[![jgswift](https://avatars.githubusercontent.com/u/661738?v=4)](https://github.com/jgswift "jgswift (17 commits)")

---

Tags

phpserializetraitclosuredelegate

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jgswift-delegatr/health.svg)

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

###  Alternatives

[kkszymanowski/traitor

Add a trait use statement to existing PHP class

1305.2M16](/packages/kkszymanowski-traitor)[iteks/laravel-enum

A comprehensive Laravel package providing enhanced enum functionalities, including attribute handling, select array conversions, and fluent facade interactions for robust enum management in Laravel applications.

2516.7k](/packages/iteks-laravel-enum)[ebidtech/collection

A set of interfaces and traits to speed up the creation of collections

13119.8k6](/packages/ebidtech-collection)

PHPackages © 2026

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