steefmin/immutable

PHP library to easily implement immutable objects

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/steefmin/immutable

1.1.1 2024-12-09 15:31 UTC

This package is auto-updated.

Last update: 2025-12-09 17:39:00 UTC


README

PHP library to easily implement immutable objects

use SteefMin\Immutable\Immutable;

/**
 * @method self withId(int $id) 
 */
final class DTO 
{
    use Immutable;
    
    public function __construct(
        private readonly int $id,
        private readonly string $value,
    ){
    }
    
    public function id(): int
    {
        return $this->id;
    }
} 

$original = new DTO(1, 'one');

$updated = $original->with('id', 2);
// or 
$updated = $original->withId(2);

echo $original->id(); // 1
echo $updated->id(); // 2