Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Provides immutable behavior for objects.

License

NotificationsYou must be signed in to change notification settings

tiny-blocks/immutable-object

Repository files navigation

License

Overview

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.

Installation

composer require tiny-blocks/immutable-object

How to use

The library provides theImmutable interface and theImmutability trait to guarantee immutability. These componentsprevent properties and collections from being modified or unset.

Concrete implementation

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)    {    }}

Handling property immutability

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

Handling collection immutability

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

License

Immutable Object is licensed underMIT.

Contributing

Please follow thecontributing guidelines tocontribute to the project.


[8]ページ先頭

©2009-2025 Movatter.jp