Download the PHP package immutablephp/immutable without Composer
On this page you can find all versions of the php package immutablephp/immutable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download immutablephp/immutable
More information about immutablephp/immutable
Files in immutablephp/immutable
Package immutable
Short Description Immutable base objects, value objects, and value bag.
License MIT
Informations about the package immutable
immutablephp/immutable
Provides truly immutable value objects and an immutable value bag, along with base Immutable and ValueObject classes for your own objects. It helps to prevent against the oversights described by the article Avoiding Quasi-Immutable Objects in PHP.
Overview
The base Immutable class protects against common oversights in PHP regarding immutables:
-
It defines
final public function __set()
andfinal public function __unset()
to prevent adding and mutating undefined properties. -
It defines
final public function offsetSet()
andfinal public function offsetUnset()
to prevent adding and mutating values via ArrayAccess. - It prevents multiple calls to
__construct()
to re-initialize the object properties.
Further, the base ValueObject class with()
method checks the types of all
incoming values to make sure they are themselves immutable. It does so via the
static methods on the Type class.
The Type class recognizes scalars and nulls as immutable. All other non-object values (such are resources and arrays) are rejected as mutable.
When it comes to objects, the Type class recognizes anything descended from
Immutable as immutable, as well as DateTimeImmutable. To allow Type to
recognize other immutable classes, call Type::register()
with a variadic list
of fully-qualified class names that you want to treat as immutable.
Making Your Own Immutable Value Objects
Note:
This package can only do so much to keep you from accidentally overlooking mutability. For example, The Immutable and ValueObject classes cannot prevent you from deliberately adding your own mutable behaviors. Likewise, it is not possible to prevent against using reflection to mutate an object from the outside.
To create your own immutable value object, extend ValueObject with your own properties.
Then add a with*()
method to allow changing of those values on a clone of the
object, using the protected with()
method on the base ValueObject.
Finally, use that method in the constructor to initialize the properties, and
call parent::__construct()
to finish initialization.
Warning:
If you do not call
parent::__construct()
then the Value Object will not know that it has been initialized, and it will be possible to call the constructor multiple time to re-initialize the object.
Now you have an immutable Value Object.
You may find it useful to add validation; do so in your with*()
methods,
either directly or by calling a validation mechanism.
Provided Immutable Value Objects
This package provides several Value Objects, both as examples and for common usage.
CreditCard
Ip
Isbn
Uri\HttpUri
Uuid
Immutable Bag
The Bag is for an arbitrary collection of immutable values, and can be useful for immutable representations of JSON data.