3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Foo { public function __construct( private readonly int $a, private readonly string $b, ) { } public function withA(int $a): self { return new self(...(get_defined_vars() + get_object_vars($this))); } public function withB(string $b): self { return new self(...(get_defined_vars() + get_object_vars($this))); } } $f = (new Foo(1, "foo")) ->withA(2) ->withB("bar"); var_dump($f);

preferences:
56.43 ms | 2682 KiB | 5 Q