steefmin/immutable

PHP library to easily implement immutable objects

1.1.1 2024-12-09 15:31 UTC

This package is auto-updated.

Last update: 2025-06-09 16:36:11 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