- Notifications
You must be signed in to change notification settings - Fork0
Provides immutable behavior for objects.
License
tiny-blocks/immutable-object
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
TheImmutable Object library ensures that objects implementing it remain immutable after initialization. Oncecreated, the state of the object cannot be modified. Any attempt to change properties or collection elements will throwan exception.
composer require tiny-blocks/immutable-object
The library provides theImmutable interface and theImmutability trait to guarantee immutability. These componentsprevent properties and collections from being modified or unset.
By implementing theImmutable interface and using theImmutability trait, you can ensure that object properties andelements in collections are immutable.
<?phpnamespaceExample;useTinyBlocks\Immutable\Immutable;useTinyBlocks\Immutable\Immutability;finalclass Orderimplements Immutable{use Immutability;publicfunction__construct(publicint$id,publicProducts$products) { }}
The Immutability trait also prevents modifications to properties of an object. Trying to modify a property after theobject is initialized will throw an exception.
$order =newOrder(id:1, products:newProducts(array: ['item1','item2']);$order->id =2;# Throws an exceptionunset($order->id);# Throws an exception
TheImmutability trait also prevents the modification of collection elements (e.g., arrays). Trying to modify orremove an element will throw an exception.
$order =newOrder(id:1, products:newProducts(array: ['item1','item2']);$order->items[0] ='item3';# Throws an exceptionunset($order->items[0]);# Throws an exception
Immutable Object is licensed underMIT.
Please follow thecontributing guidelines tocontribute to the project.
About
Provides immutable behavior for objects.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.