mbretter/stk-immutable

Stk Immutable Data objects

2.1.0 2023-07-07 05:32 UTC

This package is auto-updated.

Last update: 2024-11-09 13:13:03 UTC


README

License PHP 8 CI codecov Latest Stable Version Total Downloads

This library implements the immutable design pattern for objects holding some kind of data.

Maps

Maps can hold objects (stdClass) and/or arrays, they can be nested, very useful when reading/writing from/to noSQL databases like MongoDB.

use Stk\Immutable\Map;

$a = new Map((object)['x' => 'foo', 'y' => 'bar']);
$b = $a->set('x', 'whatever');

Calling the set method on the object $a does not modify it, a clone with the modifications is returned instead, the original Map will never be modified.

Comparing two Maps is easy, there is no need for writing complicated and resource consuming comparision functions, simply use the identical operator:

if ($a === $b) {
    ...
}