3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user = User_DTO::fromApiArray([ 'id' => 1, 'name' => 'Foo', 'username' => 'foo', 'email' => '', 'address' => [ 'street' => '', 'suite' => '', 'city' => '', 'zipcode' => '', 'geo' => [ 'lat' => '', 'lng' => '', ], ], 'phone' => '', 'website' => '', 'company' => [ 'name' => '', 'catchPhrase' => '', 'bs' => '', ], ]); echo $user->get_id() . PHP_EOL; echo $user->get_name() . PHP_EOL; echo $user->get_username() . PHP_EOL; } catch(Exception $e) { echo $e->getMessage(); } class User_DTO { private int $id; private string $name; private string $username; // force use of named constructors private function __construct() { } public static function fromApiArray($array): static { $inst = new static; $inst->id = (int) $array['id']; $inst->name = (string) $array['name']; $inst->username = (string) $array['username']; return $inst; } public function get_id(): int { return $this->id; } public function get_name(): string { return $this->name; } public function get_username(): string { return $this->username; } // etc }
Output for 8.1.23 - 8.1.28, 8.2.10 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
1 Foo foo
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 1 Foo foo
Output for 7.4.0 - 7.4.8
Parse error: syntax error, unexpected 'static' (T_STATIC) in /in/dcMeV on line 44
Process exited with code 255.
Output for 7.2.0 - 7.2.32, 7.3.0 - 7.3.20
Parse error: syntax error, unexpected 'int' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /in/dcMeV on line 36
Process exited with code 255.

preferences:
116.49 ms | 401 KiB | 90 Q