Search by

maslosoft / light

pmaselkowski

Flyweight Implementation for PHP Application Servers

Package info

github.com/Maslosoft/Light

pkg:composer/maslosoft/light

Statistics

Installs: 523

Dependents: 1

Suggesters: 0

Stars: 0

Open Issues: 0

2.0.0 2026-11-18 14:54 UTC

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.