3v4l.org

run code in 300+ PHP versions simultaneously
<?php class DTO { public function __construct( public int $a, public int $b, public int $c, public int $d, public int $e, public int $f, ) {} } $dto = new DTO(1, 2, 3, 4, 5, 6); var_dump($dto); // #PHP 8.2+, with named parameters in arrays $copy = new DTO(...(['d' => 42] + get_object_vars($dto))); var_dump($copy); // #PHP 8.0–1+, with unnamed parameters in arrays $copy2 = new DTO(...array_values(array_merge(get_object_vars($dto), ['d' => 42]))); var_dump($copy2); // #PHP 7.4– : not supported
Output for 8.0.0 - 8.0.29, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
object(DTO)#1 (6) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(4) ["e"]=> int(5) ["f"]=> int(6) } object(DTO)#2 (6) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(42) ["e"]=> int(5) ["f"]=> int(6) } object(DTO)#3 (6) { ["a"]=> int(1) ["b"]=> int(2) ["c"]=> int(3) ["d"]=> int(42) ["e"]=> int(5) ["f"]=> int(6) }
Output for 7.3.21, 7.4.0 - 7.4.25
Parse error: syntax error, unexpected 'public' (T_PUBLIC), expecting variable (T_VARIABLE) in /in/dvtO4 on line 5
Process exited with code 255.

preferences:
49.26 ms | 901 KiB | 4 Q