3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { public function __construct(public string $bar, public string $baz) {} private function cloneModify(...$args) { $new = clone($this); foreach ($args as $k => $v) { $new->$k = $v; } return $new; } public function withBar(string $bar) { return $this->cloneModify(bar: $bar); } } $f = new Foo('a', 'b'); $f2 = $f->withBar('beep'); var_dump($f2);
Output for 8.1.23 - 8.1.34, 8.2.10 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
object(Foo)#2 (2) { ["bar"]=> string(4) "beep" ["baz"]=> string(1) "b" }
Output for 7.2.0 - 7.2.34, 7.3.0 - 7.3.24, 7.4.0 - 7.4.12
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/LJkPt on line 5
Process exited with code 255.

preferences:
74.46 ms | 1158 KiB | 4 Q