vlx / php-immutable-tools
Tools for work with immutable data objects
Installs: 8
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/vlx/php-immutable-tools
Requires
- php: >=8.0
This package is auto-updated.
Last update: 2025-11-17 17:10:01 UTC
README
To make it possible to create a modified copy of an immutable object use trait, and call method with.
readonly class SomeData { use ImmutableData; public function __construct( public int $field, public AnotherData $objectField, public bool $flag = false, ) { } } $object = new SomeData(field: 1, objectField: new AnotherData(), flag: true); $newObject = $object->with(field: 2, flag: false);
You can also use rector for automatic generation phpDoc for with method in each class which use ImmutableData
<?php declare(strict_types=1); use Rector\Config\RectorConfig; use Vlx\Immutable\ImmutableModelsRector; return RectorConfig::configure() ... ->withRules([ImmutableModelsRector::class]);