3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user_data = json_encode( [ 'id' => 'a2aa06d9-b3b6-4f8c-a450-a5a37f401ff4', 'name' => 42, 'username' => (float)'NaN', 'email' => true, 'address' => [ 'street' => false, 'suite' => M_PI, 'city' => 1.5, 'zipcode' => 'The back of beyond', 'geo' => [ 'lat' => 'https://3v4l.org', 'lng' => 'looooong', ], ], 'phone' => 'E.T. Phone Home', 'website' => 0, 'company' => [ 'name' => 1.5, 'catchPhrase' => false, 'bs' => true, ], ], JSON_PRETTY_PRINT ); echo $user_data . PHP_EOL; $user_data = json_decode( $user_data, true ); $user = new User_DTO($user_data); $user_list = new User_List_DTO([ 'id' => 2, 'name' => 'Bar', 'username' => 'bar', ]); echo $user->get_id() . PHP_EOL; echo $user->get_name() . PHP_EOL; echo $user->get_username() . PHP_EOL; echo $user_list->get_id() . PHP_EOL; echo $user_list->get_name() . PHP_EOL; echo $user_list->get_username() . PHP_EOL; } catch(Exception $e) { echo $e->getMessage(); } abstract class DTO { protected $data = []; protected $schema = []; public function __construct( array $data ) { $this->validate_data_against_schema( $data, $this->schema ); $this->data = $data; } private function validate_data_against_schema( array $data, array $schema ) { foreach ( $schema as $key => $value ) { if ( ! array_key_exists( $key, $data ) ) { throw new \UnexpectedValueException( sprintf( 'Required key "%s" does not exist.', $key ) ); } // We don't want to enforce any specific type, but if we // expect a scalar, we don't want an array. if ( is_scalar( $value ) && ! is_scalar( $data[ $key ] ) ) { throw new \UnexpectedValueException( sprintf( 'Key "%s" must be scalar, %s provided.', $key, gettype( $data[ $key ] ) ) ); } // Same the other way around. If we expect an array, // we don't want anything else. if ( is_array( $value ) && ! is_array( $data[ $key ] ) ) { throw new \UnexpectedValueException( sprintf( 'Key "%s" must be an array, %s provided.', $key, gettype( $data[ $key ] ) ) ); } // Traverse down the children, recursively. if ( is_array( $value ) ) { $this->validate_data_against_schema( $data[ $key ], $value ); } } } } class User_DTO extends DTO { protected $schema = [ 'id' => '', 'name' => '', 'username' => '', 'email' => '', 'address' => [ 'street' => '', 'suite' => '', 'city' => '', 'zipcode' => '', 'geo' => [ 'lat' => '', 'lng' => '', ], ], 'phone' => '', 'website' => '', 'company' => [ 'name' => '', 'catchPhrase' => '', 'bs' => '', ], ]; public function get_id(): string { return (string) $this->data['id']; } public function get_name(): string { return (string) $this->data['name']; } public function get_username(): string { return (string) $this->data['username']; } // etc } class User_List_DTO extends DTO { protected $schema = [ 'id' => '', 'name' => '', 'username' => '', ]; public function get_id(): string { return (string) $this->data['id']; } public function get_name(): string { return (string) $this->data['name']; } public function get_username(): string { return (string) $this->data['username']; } }

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0150.00018.43
8.3.50.0090.00616.39
8.3.40.0160.00619.09
8.3.30.0060.00918.72
8.3.20.0000.00824.18
8.3.10.0000.00824.66
8.3.00.0050.00326.16
8.2.180.0090.00625.92
8.2.170.0090.00618.96
8.2.160.0060.00822.96
8.2.150.0070.00025.66
8.2.140.0080.00024.66
8.2.130.0080.00026.16
8.2.120.0070.00026.35
8.2.110.0040.00721.95
8.2.100.0080.00318.03
8.2.90.0030.00517.97
8.2.80.0030.00619.02
8.2.70.0000.00917.75
8.2.60.0080.00017.50
8.2.50.0060.00318.05
8.2.40.0000.00818.47
8.2.30.0000.00718.05
8.2.20.0000.00718.12
8.2.10.0040.00419.31
8.2.00.0000.00719.40
8.1.280.0090.01225.92
8.1.270.0050.00323.99
8.1.260.0060.00326.35
8.1.250.0090.00028.09
8.1.240.0030.00622.19
8.1.230.0070.00420.99
8.1.220.0060.00317.76
8.1.210.0000.00818.77
8.1.200.0000.00817.55
8.1.190.0080.00017.35
8.1.180.0040.00418.10
8.1.170.0040.00418.51
8.1.160.0000.00719.00
8.1.150.0000.00718.98
8.1.140.0040.00418.97
8.1.130.0000.00720.19
8.1.120.0040.00417.53
8.1.110.0030.00517.50
8.1.100.0070.00017.51
8.1.90.0050.00317.36
8.1.80.0000.01117.48
8.1.70.0070.00017.52
8.1.60.0040.00817.65
8.1.50.0090.00017.54
8.1.40.0040.00417.60
8.1.30.0090.00017.68
8.1.20.0040.00417.57
8.1.10.0030.01017.52
8.1.00.0050.00317.55
8.0.300.0040.00420.22
8.0.290.0040.00416.75
8.0.280.0030.00318.68
8.0.270.0070.00018.05
8.0.260.0060.00018.43
8.0.250.0000.00717.07
8.0.240.0000.00617.07
8.0.230.0000.00717.08
8.0.220.0000.00717.01
8.0.210.0000.00816.91
8.0.200.0070.00017.01
8.0.190.0040.00417.07
8.0.180.0000.00817.00
8.0.170.0000.00916.90
8.0.160.0000.00817.12
8.0.150.0040.00417.02
8.0.140.0040.00416.87
8.0.130.0000.00613.43
8.0.120.0040.00416.85
8.0.110.0040.00417.06
8.0.100.0040.00417.00
8.0.90.0050.00216.96
8.0.80.0050.00916.93
8.0.70.0070.00016.96
8.0.60.0050.00316.80
8.0.50.0050.00216.89
8.0.30.0070.01017.08
8.0.20.0130.01117.06
8.0.10.0040.00417.19
8.0.00.0130.01316.82
7.4.330.0050.00015.55
7.4.320.0060.00016.56
7.4.300.0000.00716.50
7.4.290.0000.00816.39
7.4.280.0050.00316.50
7.4.270.0000.00716.56
7.4.260.0050.00013.25
7.4.250.0040.00416.60
7.4.240.0030.00516.61
7.4.230.0050.00316.57
7.4.220.0050.00216.62
7.4.210.0080.00816.57
7.4.200.0000.00716.30
7.4.130.0090.00916.48
7.4.120.0070.01116.45
7.4.110.0140.00316.57
7.4.100.0060.01116.33
7.4.90.0120.00616.52
7.4.80.0060.01216.26
7.4.70.0060.01316.59
7.4.60.0150.00316.69
7.4.50.0140.00616.68
7.4.40.0170.00016.54
7.4.30.0040.01416.39
7.4.20.0120.00916.74
7.4.10.0060.01216.52
7.4.00.0130.00316.54
7.3.330.0050.00316.32
7.3.320.0030.00313.30
7.3.310.0050.00316.50
7.3.300.0000.00716.48
7.3.290.0080.01016.47
7.3.280.0120.00816.45
7.3.260.0090.01116.65
7.3.230.0140.00316.46
7.3.210.0060.01116.52
7.3.200.0110.00716.43
7.3.190.0140.00716.62
7.3.180.0030.01616.67
7.3.170.0120.01216.61
7.3.160.0150.00616.48
7.3.150.0100.00716.38
7.3.140.0060.01016.63
7.3.130.0000.01916.68
7.3.120.0040.01516.66
7.3.110.0110.00816.70
7.3.100.0090.00916.39
7.3.90.0060.01216.70
7.3.80.0070.01116.35
7.3.70.0120.00916.60
7.3.60.0160.00316.53
7.3.50.0160.00316.29
7.3.40.0140.00416.46
7.3.30.0070.01716.48
7.3.20.0140.00416.45
7.3.10.0060.01216.43
7.3.00.0070.01116.46
7.2.330.0120.00616.64
7.2.320.0140.01016.90
7.2.310.0090.00916.50
7.2.300.0130.00616.54
7.2.290.0060.01216.82
7.2.280.0090.00916.58
7.2.270.0100.00716.75
7.2.260.0030.01416.69
7.2.250.0150.00316.71
7.2.240.0090.00916.73
7.2.230.0090.00916.82
7.2.220.0180.00016.79
7.2.210.0110.00816.83
7.2.200.0140.00716.84
7.2.190.0100.01316.60
7.2.180.0110.00716.57
7.2.170.0030.01916.83
7.2.160.0060.01516.67
7.2.150.0040.01416.71
7.2.140.0070.01116.87
7.2.130.0120.00616.80
7.2.120.0100.00716.77
7.2.110.0030.01616.83
7.2.100.0090.00916.67
7.2.90.0060.01216.68
7.2.80.0030.01616.54
7.2.70.0200.01016.79
7.2.60.0090.00916.62
7.2.50.0100.01316.88
7.2.40.0170.00316.80
7.2.30.0160.00716.77
7.2.20.0130.00716.72
7.2.10.0160.00316.73
7.2.00.0120.01216.67

preferences:
40.35 ms | 400 KiB | 5 Q