fratily/clone-with

0.2.0 2023-11-05 12:21 UTC

This package is auto-updated.

Last update: 2024-05-05 13:31:09 UTC


README

English README.md

fratily/clone-withはPHPのオブジェクトクローンを拡張する関数を追加するライブラリです。

オブジェクトをクローンするときに任意のプロパティの値を書き換えられるようにします。 これは書き換えるプロパティがreadonlyでもprivateでも機能します。

NOTE: 現時点では継承しているスーパークラスのprivateプロパティを書き換えることはできません。 これは「スーパークラスのプライベートプロパティは隠ぺいされているし、外部から触られるべきではないんじゃね?」という作者の考えに基づいています。

使い方

このライブラリは、readonlyなプロパティで構成される不変オブジェクトをより使いやすくするために作成されました。

class Foo {
  public function __construct(
    public readonly string $value_string,
    public readonly int $value_int,
  ) {}

  public function withString(string $new_value)
  {
    return clone_with_new_props($this, ['value_string' => $new_value]);
  }

  public function withInt(int $new_value)
  {
    return clone_with_new_props($this, ['value_int' => $new_value]);
  }
}