serafim / immutable
Simple helper to ensure immutable objects
Installs: 188
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 3
Forks: 0
Open Issues: 0
pkg:composer/serafim/immutable
Requires
- php: ^7.2|^8.0
Requires (Dev)
- phpunit/phpunit: ^7.2|^8.0
- sensiolabs/security-checker: ^6.0
- squizlabs/php_codesniffer: ^3.5
- symfony/var-dumper: ~4.3|~5.0
- vimeo/psalm: ^3.6
This package is auto-updated.
Last update: 2020-08-09 01:56:39 UTC
README
Installation
Install via Composer:
composer require serafim/immutable
Usage
To ensure immunity of objects, you just need to wrap any code of your method in a closure.
Mutable object example:
class Example { private int $value = 42; public function update(int $newValue): self { $this->value = $newValue; return $this; } }
Making it immutable:
class Example { private int $value = 42; // Sample #1 (PHP 7.4+) public function with(int $newValue): self { return immutable(fn () => $this->value = $newValue); } // Sample #2 (PHP 7.3 and below) public function with(int $newValue): self { return immutable(function (): void { $this->value = $newValue; }); } }
That`s all!
Features
- ☑ Full PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0 (dev) compatibility (proof)
- ☑ PSR-4
- ☑ PSR-12
- ☑ Convenient helpers that don't break code =)
- ☑ Feel the power of immutable objects!
License
See LICENSE