3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo_DTO { public function __construct( array $data ) { array_map( function ( $r ) use ( $data ) { if ( ! array_key_exists( $r, $data ) ) { throw new \UnexpectedValueException( 'Required key "%s" does not exist.', $r ); } }, [ 'id', 'name', 'username' ] ); $this->id = $data['id']; $this->name = $data['name']; $this->username = $data['username']; } public function get_id() { return (string) $this->id; } public function get_name() { return (string) $this->name; } public function get_username() { return (string) $this->username; } } $foo = new Foo_DTO(['id' => 123, 'name' => 'Foo', 'username' => 'foo']); echo $foo->get_id() . PHP_EOL; echo $foo->get_name() . PHP_EOL; echo $foo->get_username() . PHP_EOL;
Output for 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
Deprecated: Creation of dynamic property Foo_DTO::$id is deprecated in /in/cpWcq on line 11 Deprecated: Creation of dynamic property Foo_DTO::$name is deprecated in /in/cpWcq on line 12 Deprecated: Creation of dynamic property Foo_DTO::$username is deprecated in /in/cpWcq on line 13 123 Foo foo
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33
123 Foo foo

preferences:
143.97 ms | 408 KiB | 5 Q