serafim/immutable

This package is abandoned and no longer maintained. The author suggests using the phpfn/immutable package instead.

Simple helper to ensure immutable objects

1.0.0 2019-11-22 01:59 UTC

This package is auto-updated.

Last update: 2020-08-09 01:56:39 UTC


README

Build Status 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f62323630366534616130643730333037313938642f6d61696e7461696e6162696c697479 68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f62323630366534616130643730333037313938642f746573745f636f766572616765

PHP 7.1+ Latest Stable Version Total Downloads License MIT

Installation

Install via Composer:

composer require serafim/immutable

Usage

To ensure immunity of objects, you just need to wrap any code of your method in a closure.

Mutable object example:

class Example
{
    private int $value = 42;

    public function update(int $newValue): self
    {
        $this->value = $newValue;
    
        return $this;
    }
}

Making it immutable:

class Example
{
    private int $value = 42;

    // Sample #1 (PHP 7.4+)
    public function with(int $newValue): self
    {
        return immutable(fn () => $this->value = $newValue);
    }

    // Sample #2 (PHP 7.3 and below)
    public function with(int $newValue): self
    {
        return immutable(function (): void { $this->value = $newValue; });
    }

}

That`s all!

Features

  • ☑ Full PHP 7.2, PHP 7.3, PHP 7.4 and PHP 8.0 (dev) compatibility (proof)
  • PSR-4
  • PSR-12
  • ☑ Convenient helpers that don't break code =)
  • ☑ Feel the power of immutable objects!

License

See LICENSE