kamilwylegala / immutable-setter
Utility class for PHP that helps making your classes immutable.
Installs: 939
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/kamilwylegala/immutable-setter
Requires
- php: >=5.4.0
Requires (Dev)
- phpunit/phpunit: 5.4.*
This package is not auto-updated.
Last update: 2025-12-22 05:18:31 UTC
README
Tiny utillity class that helps you making your classes immutable by adding wither methods.
Inspired by Lombok's @Wither annotation.
It lets you easily add witherX methods to clone object with modified one property.
Installation
Use composer to get library from packagist:
$ php composer.phar require kamilwylegala/immutable-setter
Usage
- Add
witherfield to your class. - Assign
new Wither($this, ["arg1", "arg2"])to this field and provide constructor schema with proper order of arguments. You can also skip second argument and letWitherresolve constructor arguments automatically. - Add public
withArg1method to your class and put:
return $this->wither->getInstance("arg1", $newArg1)
- Running
$valueObject->withArg1($newArg1)will create copy of your object with changed$arg1field.
Example
use KamilWylegala\ImmutableSetter\Wither; class Person { private $name; private $age; private $wither; public function __construct($name, $age) { $this->name = $name; $this->age = $age; $this->wither = new Wither($this, ["name", "age"]); //Second param is optional. } public function withName($newName) { return $this->wither->getInstance("name", $newName); } }
Tests
Install dev dependencies and run in root:
$ vendor/bin/phpunit
Licence
MIT