react/partial
Lightweight partial function application for PHP. Pre-fill (bind) some arguments of any callable and get back a new callable with fewer required params—handy for async callbacks and event handlers where you need to pass extra context without verbose closures.
$middleware = bind([AuthMiddleware::class, 'handle'], $user);
$this binding or Closure::bindTo() are cumbersome."react/partial": "^2.0"). No runtime dependencies beyond PHP 7.1+.react/http, react/socket, or react/promise.spatie/react or react/laravel.bind for async callbacks, placeholder for array_map").$processOrderWithUser).Logger::debug("Bound user ID: {$user->id}")).react/http)?app()->call()) in specific cases?// Before: Mocking a closure
$this->expects($mock->handle($contents))->once();
// After: Mocking a bound function
$bound = bind([$mock, 'handle'], $user);
$this->expects($mock->handle($contents))->with($user)->once();
Closure::bindTo(), app()->call()) or packages like php-di/container achieve similar goals with less learning curve?amp/async-function (Amp’s partial application) a better fit if using Amp instead of ReactPHP?| Laravel Component | Partial Application Fit | Example Use Case |
|---|---|---|
| ReactPHP Extensions | High | Async HTTP routes, WebSocket handlers, or background jobs with pre-bound contexts. |
| Middleware | Medium | Bind request/response objects or user auth data to middleware logic. |
| Event Listeners | Medium | Pre-fill event data in listeners (e.g., $handleOrderCreated = bind([...], $order)). |
| Queue Workers | High (if async) | Bind job metadata to callback-based workers (e.g., bind([Worker::class, 'handle'], $job->payload)). |
| Eloquent Models | Low | Minimal value unless paired with async observers or custom query builders. |
| API Controllers | Low | Closures or method binding suffice; partial application adds no benefit. |
// Before
$socket->on('message', function (ConnectionInterface $conn, $message) {
$this->processMessage($conn->getRemoteAddress(), $message);
});
// After
$processMessage = bind([$this, 'processMessage'], $conn->getRemoteAddress());
$socket->on('message', $processMessage);
$middleware = bind([AuthMiddleware::class, 'handle'], $request->user());
$worker = bind([OrderProcessor::class, 'process'], $order->id);
$queue->push($worker);
/**
* @param callable($contents): void $callback
*/
public function downloadFile(string $filename, callable $callback): void {}
trait PartialApplication {
protected function bindToContext(callable $callback, ...$args) {
return React\Partial\bind($callback, ...$args);
}
}
spatie/react for Laravel-ReactPHP bridges.react/laravel for tighter integration.function adaptToPartial(callable $fn, ...$args) {
return React\Partial\bind($fn, ...$args);
}
bind/bind_right if using PHP 7.4+ with type hints.composer.json:
{
"require": {
"react/partial": "^2.0",
"react/http": "^1.0",
"spatie/react": "^1.0"
}
}
$server = new React\Http\Server($loop);
$server->on('request', bind([$this, 'handleRequest'], $requestContext));
placeholder with variable argument counts (e.g., array_map).$firstChar = React\Partial\bind('substr', React\Partial\placeholder(), 0, 1);
PARTIAL_APPLICATION.md to the codebase with:
bind_right for readability).How can I help you explore Laravel packages today?