<?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);
You have javascript disabled. You will not be able to edit any code.