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 git.master, git.master_jit, rfc.property-hooks
1 Foo foo

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
32.81 ms | 401 KiB | 8 Q