3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user = new User_DTO([ '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, ], ]); $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(): int { return (int) $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(): int { return (int) $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.5.30.0110.00617.18
8.5.20.0090.01016.54
8.5.10.0120.00616.48
8.5.00.0130.01020.18
8.4.180.0150.00719.72
8.4.170.0120.01020.48
8.4.160.0130.01023.78
8.4.150.0060.00416.80
8.4.140.0120.01117.77
8.4.130.0080.00417.73
8.4.120.0140.00620.95
8.4.110.0060.00422.77
8.4.100.0130.00717.77
8.4.90.0060.00718.95
8.4.80.0110.01018.06
8.4.70.0140.00718.04
8.4.60.0130.00820.72
8.4.50.0150.00617.86
8.4.40.0070.01017.81
8.4.30.0030.01720.61
8.4.20.0160.00320.68
8.4.10.0070.01120.74
8.3.300.0110.00920.71
8.3.290.0120.01120.74
8.3.280.0090.01418.63
8.3.270.0100.00916.70
8.3.260.0110.00916.75
8.3.250.0130.00518.91
8.3.240.0120.00916.88
8.3.230.0130.00716.86
8.3.220.0090.00917.27
8.3.210.0140.00716.87
8.3.200.0060.00216.72
8.3.190.0040.00419.13
8.3.180.0130.00616.58
8.3.170.0030.01516.75
8.3.160.0110.00716.63
8.3.150.0190.00417.16
8.3.140.0080.00016.63
8.3.130.0030.00518.41
8.3.120.0090.00918.97
8.3.110.0060.00318.30
8.3.100.0070.00424.06
8.3.90.0000.00826.77
8.3.80.0030.00619.36
8.3.70.0140.00718.68
8.3.60.0110.00416.49
8.3.50.0130.00523.70
8.3.40.0070.01518.62
8.3.30.0110.00418.79
8.3.20.0070.00024.18
8.3.10.0080.00024.66
8.3.00.0040.00426.16
8.2.300.0120.01022.16
8.2.290.0120.00920.34
8.2.280.0120.00820.01
8.2.270.0110.00716.84
8.2.260.0110.00419.16
8.2.250.0050.00316.89
8.2.240.0030.00617.44
8.2.230.0030.00720.94
8.2.220.0060.00337.54
8.2.210.0080.00026.77
8.2.200.0060.00318.88
8.2.190.0110.01118.54
8.2.180.0120.00325.92
8.2.170.0030.01618.88
8.2.160.0070.00722.96
8.2.150.0000.00825.66
8.2.140.0000.00824.66
8.2.130.0050.00226.16
8.2.120.0040.00426.35
8.2.110.0060.00321.96
8.2.100.0070.00717.97
8.2.90.0080.00017.75
8.2.80.0050.00318.90
8.2.70.0000.00817.63
8.2.60.0040.00417.63
8.2.50.0040.00417.75
8.2.40.0030.00619.60
8.2.30.0030.00518.00
8.2.20.0000.00718.25
8.2.10.0000.00819.25
8.2.00.0050.00219.34
8.1.340.0110.00917.81
8.1.330.0140.00622.00
8.1.320.0120.00716.23
8.1.310.0140.00418.53
8.1.300.0080.00016.23
8.1.290.0090.00030.84
8.1.280.0090.01225.92
8.1.270.0090.00023.99
8.1.260.0040.00428.09
8.1.250.0040.00428.09
8.1.240.0090.00023.83
8.1.230.0000.01119.22
8.1.220.0040.00417.79
8.1.210.0000.00818.77
8.1.200.0060.00617.35
8.1.190.0000.00817.35
8.1.180.0000.00918.10
8.1.170.0080.00017.62
8.1.160.0040.00418.82
8.1.150.0040.00418.85
8.1.140.0040.00418.79
8.1.130.0040.00420.19
8.1.120.0050.00317.54
8.1.110.0040.00417.37
8.1.100.0040.00417.45
8.1.90.0000.00717.37
8.1.80.0080.00317.49
8.1.70.0000.00717.43
8.1.60.0070.00417.56
8.1.50.0040.00417.51
8.1.40.0030.00517.52
8.1.30.0060.00317.66
8.1.20.0040.00417.63
8.1.10.0030.00517.61
8.1.00.0000.00717.42
8.0.300.0000.00818.77
8.0.290.0090.00016.75
8.0.280.0000.00818.47
8.0.270.0030.00318.09
8.0.260.0000.00618.43
8.0.250.0030.00316.95
8.0.240.0070.00017.02
8.0.230.0070.00017.01
8.0.220.0050.00216.85
8.0.210.0000.00717.00
8.0.200.0070.00016.90
8.0.190.0040.00416.99
8.0.180.0000.00716.98
8.0.170.0040.00417.00
8.0.160.0040.00417.01
8.0.150.0000.00716.95
8.0.140.0050.00216.98
8.0.130.0000.00613.48
8.0.120.0000.00816.92
8.0.110.0040.00416.86
8.0.100.0000.00716.89
8.0.90.0040.00416.97
8.0.80.0070.00816.93
8.0.70.0030.00516.93
8.0.60.0020.00516.91
8.0.50.0000.00716.85
8.0.30.0100.01017.11
8.0.20.0120.01417.22
8.0.10.0040.00416.98
8.0.00.0000.01817.05
7.4.330.0030.00315.55
7.4.320.0030.00316.47
7.4.300.0030.00316.68
7.4.290.0000.00716.43
7.4.280.0070.00016.61
7.4.270.0030.00316.44
7.4.260.0000.00613.30
7.4.250.0030.00316.37
7.4.240.0030.00316.44
7.4.230.0000.00716.44
7.4.220.0020.00516.63
7.4.210.0060.00916.66
7.4.200.0030.00316.54
7.4.130.0070.01316.67
7.4.120.0000.01816.52
7.4.110.0100.00716.51
7.4.100.0110.00616.51
7.4.90.0070.01016.48
7.4.80.0170.00916.35
7.4.70.0260.01916.65
7.4.60.0260.01716.64
7.4.50.0300.01516.44
7.4.40.0280.00816.49
7.4.30.0140.02416.59
7.4.20.0270.01316.64
7.4.10.0280.00916.58
7.4.00.0110.01116.60
7.3.330.0030.00316.55
7.3.320.0030.00313.46
7.3.310.0030.00316.56
7.3.300.0000.00716.50
7.3.290.0030.01116.48
7.3.280.0070.01316.44
7.3.260.0150.00316.52
7.3.230.0100.00716.43
7.3.210.0060.01116.30
7.3.200.0290.00316.48
7.3.190.0280.01716.56
7.3.180.0300.01416.35
7.3.170.0320.01416.53
7.3.160.0160.02616.48
7.3.150.0310.01216.38
7.3.140.0300.01016.61
7.3.130.0280.01016.40
7.3.120.0310.01216.30
7.3.110.0370.00616.65
7.3.100.0350.01216.56
7.3.90.0300.00716.68
7.3.80.0290.00916.54
7.3.70.0230.01316.56
7.3.60.0290.01116.56
7.3.50.0290.01116.48
7.3.40.0230.01016.59
7.3.30.0240.01516.43
7.3.20.0260.01616.46
7.3.10.0060.01816.12
7.3.00.0210.00316.48
7.2.330.0160.00916.76
7.2.320.0260.00816.84
7.2.310.0320.01316.82
7.2.300.0280.01216.78
7.2.290.0230.01616.57
7.2.280.0270.01216.86
7.2.270.0330.00316.75
7.2.260.0290.00916.82
7.2.250.0290.01016.48
7.2.240.0290.01216.85
7.2.230.0260.01316.64
7.2.220.0260.01016.75
7.2.210.0370.00016.74
7.2.200.0310.00916.56
7.2.190.0340.00616.82
7.2.180.0290.01016.87
7.2.170.0260.00916.68
7.2.160.0230.02016.61
7.2.150.0210.00916.59
7.2.140.0200.00616.70
7.2.130.0120.01216.90
7.2.120.0130.01316.77
7.2.110.0130.01616.63
7.2.100.0140.01116.70
7.2.90.0080.01716.90
7.2.80.0100.01316.69
7.2.70.0180.00416.70
7.2.60.0240.01516.83
7.2.50.0150.01216.77
7.2.40.0130.01016.89
7.2.30.0190.00316.83
7.2.20.0110.01116.60
7.2.10.0090.01416.63
7.2.00.0100.01016.75

preferences:
50.51 ms | 1474 KiB | 5 Q