serafim/immutable

此包已被废弃,不再维护。作者建议使用 phpfn/immutable 包。

确保不可变对象的简单助手

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

安装

通过 Composer 安装

composer require serafim/immutable

使用方法

为确保对象的不可变性,只需将您的任何方法代码包裹在一个闭包中即可。

可变对象示例

class Example
{
    private int $value = 42;

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

使其不可变

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

}

就这些了!

功能

  • ☑ 完全兼容 PHP 7.2, PHP 7.3, PHP 7.4 和 PHP 8.0 (dev) (证据)
  • PSR-4
  • PSR-12
  • ☑ 方便的助手,不会破坏代码 =)
  • ☑ 感受不可变对象的强大力量!

许可

查看 LICENSE