PHPackages                             mantas-done/short-closures - 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. mantas-done/short-closures

Abandoned → [mantas-done/short-closures](/?search=mantas-done%2Fshort-closures)Library[Utility &amp; Helpers](/categories/utility)

mantas-done/short-closures
==========================

Short closures for PHP

v1.0.2(8y ago)117PHP

Since Dec 17Pushed 8y ago1 watchersCompare

[ Source](https://github.com/mantas-done/short-closures)[ Packagist](https://packagist.org/packages/mantas-done/short-closures)[ RSS](/packages/mantas-done-short-closures/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Short Closure syntax for PHP
----------------------------

[](#short-closure-syntax-for-php)

Currently PHP doesn't support short closures natively. But what if we could use it today?

```
// instead of this code:
$totalRevenue = $employees
    ->filter(function ($employee) {
        return $employee->district == 'Amsterdam';
    })->flatMap(function ($employee) {
        return $employee->customers;
    })->map(function ($customer) {
        return $customer->orders->sum('total');
    })->filter(function ($customerTotal) {
        return $customerTotal >= 75000;
    })->sum();

// you can do:
$totalRevenue = $employees
    ->filter('$employee->district == "Amsterdam"')
    ->flatMap('$employee->customers')
    ->map('$customer->orders->sum("total")')
    ->filter('$customerTotal >= 75000')
    ->sum();
```

How it works? This library has a function that converts string to regular PHP anonymous function.

```
// instead of this code:
$anonymous_function = function ($x) {
   return $x * 2;
}

// you can do:
$anonymous_function = c('$x ~> $x * 2');

// or even shorter:
$anonymous_function = c('$x * 2');

// usage
echo $anonymous_function(3); // output: 6
```

Instalation
-----------

[](#instalation)

```
composer require mantas-done/short-closures

```

Syntax
------

[](#syntax)

```
// all this syntax is valid
c('$x * 2');
c('$x ~> $x * 2');
c('($x) ~> $x * 2');
c('$x ~> {return $x * 2;}');
c('($x) ~> {return $x * 2;}');
c('($v, $k) ~> $v == 2 && $k == 1');
c('($v, $k) ~> {return $v == 2 && $k == 1;}');
c('($v, $k) ~> {$v2 = $v * 2; $k2 = $k * 2; return $v2 == 4 && $k2 == 2;}');
```

Usage
-----

[](#usage)

This package can be used in any project, but the biggest advantage would be achieved if it would be tightly integrated into other packages.
For example if it would be integrated into Laravel Collection package, c() function call could be removed.
Currently you can use c() helper to convert your strings to closure:

```
$totalRevenue = $employees
    ->filter(c('$employee->district == "Amsterdam"'))
    ->flatMap(c('$employee->customers'))
    ->map(c('$customer->orders->sum("total")'))
    ->filter(c('$customerTotal >= 75000'))
    ->sum();
```

Or you can replace your Laravel Collection class with provided CollectionWithShortClosure class. Then you can write code without c() helper.
CollectionWithShortClosure can be used only with PHP 7.2 or above.

```
$totalRevenue = $employees
    ->filter('$employee->district == "Amsterdam"')
    ->flatMap('$employee->customers')
    ->map('$customer->orders->sum("total")')
    ->filter('$customerTotal >= 75000')
    ->sum();
```

Short closures could be integrated into Laravel Eloquent. But I don't provide this integration.

```
// instead of this code:
$users = User::whereHas('comments', function ($q) {
    $q->published();
})->get();

// it could be replaced with:
$users = User::whereHas('comments', 'published()')->get();

// ofcouse you can use it without tight integration, but it doesn't look nice
$users = User::whereHas('comments', c('$q->published()'))->get();
```

Other
-----

[](#other)

More info about PHP short closures [https://wiki.php.net/rfc/short\_closures](https://wiki.php.net/rfc/short_closures)

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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 ~0 days

Total

3

Last Release

3117d ago

### Community

Maintainers

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

---

Top Contributors

[![mantas-done](https://avatars.githubusercontent.com/u/17127206?v=4)](https://github.com/mantas-done "mantas-done (4 commits)")

### Embed Badge

![Health badge](/badges/mantas-done-short-closures/health.svg)

```
[![Health](https://phpackages.com/badges/mantas-done-short-closures/health.svg)](https://phpackages.com/packages/mantas-done-short-closures)
```

###  Alternatives

[karpy47/php-mqtt-client

MQTT 3.1.1 library for PHP with TLS support

3788.3k1](/packages/karpy47-php-mqtt-client)[nyholm/append-query-string

A simple function that lets you add a query string to an existing URL

19287.2k2](/packages/nyholm-append-query-string)[godbout/alfred-workflow-scriptfilter

Generate Alfred 3 or 4 Workflow Results in PHP with a laugh.

173.7k1](/packages/godbout-alfred-workflow-scriptfilter)

PHPackages © 2026

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