3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Thing { private $foo; private $bar; private $qux; public function __construct($foo, $bar, $qux) { $this->foo = $foo; $this->bar = $bar; $this->qux = $qux; } } class Builder { private $foo; private $bar; public function withFoo($foo) { $this->foo = $foo; return $this; } public function withBar($bar) { $this->bar = $bar; return $this; } public function withQux($qux) { try { return new Thing($this->foo, $this->bar, $qux); } finally { $this->reset(); } } private function reset() { $this->foo = null; $this->bar = null; } } function test(Builder $builder) { return [ $builder->withFoo('a')->withBar('b')->withQux('c'), $builder->withFoo('d')->withQux('e'), $builder->withQux('f'), ]; } var_dump(test(new Builder()));

preferences:
25.45 ms | 404 KiB | 5 Q