maslosoft / light
Flyweight Implementation for PHP Application Servers
Requires
- php: >=8.5
Requires (Dev)
- codeception/codeception: ^5.3
- codeception/module-asserts: ^3.3
- phpstan/phpstan: ^2.1
Suggests
- ext-openswoole: Coroutine-local flyweights with an OpenSwoole release supporting PHP 8.5+.
- ext-swoole: Coroutine-local flyweights with a Swoole release supporting PHP 8.5+.
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-11-18 15:52:04 UTC
README
Light provides flyweight objects scoped to the current Swoole or OpenSwoole coroutine, with ordinary PHP storage when no coroutine is active. Repeated calls for the same class and instance ID return the same object within that scope.
Choose scope per concern: coroutine-local state can use Light while work such as asset publishing is shared across the instances using the same asset version and destination. Choose what to share explains those boundaries.
Requires PHP 8.5 or newer. Coroutine extensions are optional.
composer require maslosoft/light
<?php declare(strict_types=1); require 'vendor/autoload.php'; use Maslosoft\Light\Traits\Fly; use Maslosoft\Light\Traits\NewInstance; final class Formatter { use Fly; use NewInstance; public function __construct(public readonly string|int|float $id) { } } $formatter = Formatter::fly(); assert($formatter === Formatter::fly()); assert($formatter !== Formatter::fly('reports'));
An active coroutine has its own instances. Outside coroutines, instances remain in PHP static storage for the lifetime of the execution; long-running workers should manage that lifetime explicitly.
Read Getting started, Flyweight identity and construction, Contexts and cleanup, Development, or Migration to PHP 8.5.
Licensed under the MIT license.