{
    "href": "/post/2019/02/04/immutability-package-for-php/",
    "relId": "2019/02/04/immutability-package-for-php",
    "title": "Immutable Objects for PHP",
    "author": "pmjones",
    "markup": "html",
    "tags": [
        {
            "href": "/tag/php/",
            "relId": "php",
            "title": "PHP",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        },
        {
            "href": "/tag/programming/",
            "relId": "programming",
            "title": "Programming",
            "author": null,
            "created": null,
            "updated": [],
            "markup": "markdown"
        }
    ],
    "created": "2019-02-04 20:53:46 UTC",
    "updated": [
        "2019-02-04 20:53:46 UTC"
    ],
    "html": "<p>The new <a href=\"https://github.com/immutablephp/immutable\">immutablephp/immutable</a> package provides truly immutable value objects and an immutable value bag, along with base <em>Immutable</em> and <em>ValueObject</em> classes for your own objects. It helps to prevent against the oversights described by my earlier article <a href=\"http://paul-m-jones.com/archives/6400\">Avoiding Quasi-Immutable Objects in PHP</a>. <span style=\"font-size:85%\">(Of course, it cannot prevent you from deliberately adding your own mutable behaviors, or from using reflection to mutate an object from the outside. If you do those things, you get what you deserve.)</span></p>\n<p>Its base <em>Immutable</em> class protects against common oversights in PHP regarding immutables:</p>\n<ul>\n<li>\n<p>It defines <code>final public function __set()</code> and <code>final public function __unset()</code> to prevent adding and mutating undefined properties.</p>\n</li>\n<li>\n<p>It defines <code>final public function offsetSet()</code> and <code>final public function offsetUnset()</code> to prevent adding and mutating values via <em>ArrayAccess</em>.</p>\n</li>\n<li>\n<p>It prevents multiple calls to <code>__construct()</code> to re-initialize the object properties.</p>\n</li>\n</ul>\n<p>Further, the base <em>ValueObject</em> class <code>with()</code> method checks the types of all incoming values to make sure they are themselves immutable. It does so via static methods on the <em>Type</em> class.</p>\n<p>The <em>Type</em> class recognizes scalars and nulls as immutable. All other non-object values (such are resources and arrays) are rejected as mutable.</p>\n<p>When it comes to objects, the <em>Type</em> class recognizes anything descended from <em>Immutable</em> as immutable, as well as <em>DateTimeImmutable</em>. To allow <em>Type</em> to recognize other immutable classes, call <code>Type::register()</code> with a variadic list of fully-qualified class names that you want to treat as immutable.</p>\n<p>After installing <a href=\"https://packagist.org/packages/immutablephp/immutable\">immutablephp/immutable</a> via Composer, you can use it to <a href=\"https://github.com/immutablephp/immutable#making-your-own-immutable-value-objects\">create your own immutable Value Objects</a>, or consume one of the <a href=\"https://github.com/immutablephp/immutable#provided-immutable-value-objects\">provided Value Objects</a>:</p>\n<ul>\n<li><a href=\"https://github.com/immutablephp/immutable#creditcard\">CreditCard</a></li>\n<li><a href=\"https://github.com/immutablephp/immutable#email\">Email</a></li>\n<li><a href=\"https://github.com/immutablephp/immutable#ip\">Ip</a></li>\n<li><a href=\"https://github.com/immutablephp/immutable#isbn\">Isbn</a></li>\n<li><a href=\"https://github.com/immutablephp/immutable#urihttpuri\">Uri\\HttpUri</a></li>\n<li><a href=\"https://github.com/immutablephp/immutable#cuid\">Uuid</a></li>\n</ul>\n<p>There is also an <a href=\"https://github.com/immutablephp/immutable#immutable-bag\">immutable value Bag</a> for an arbitrary collection of immutable values; it can be useful for immutable representations of JSON data.</p>\n<p>If you need truly immutable objects for your PHP project, give <a href=\"https://github.com/immutablephp/immutable\">immutablephp/immutable</a> a try!</p>\n<hr>\n<p>Read the Reddit commentary on this post <a href=\"https://www.reddit.com/r/PHP/comments/an7fst/immutable_objects_for_php/\">here.</a></p>\n"
}
