3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user = new User_DTO([ 'id' => 1, 'name' => 'Foo', 'username' => 'foo', 'email' => '', 'address' => [ 'street' => '', 'suite' => '', 'city' => '', 'zipcode' => '', 'geo' => [ 'lat' => '', 'lng' => '', ], ], 'phone' => '', 'website' => '', 'company' => [ 'name' => '', 'catchPhrase' => '', 'bs' => '', ], ]); $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.0090.00917.70
8.5.20.0100.00816.79
8.5.10.0120.00616.65
8.5.00.0120.01020.18
8.4.180.0110.01119.59
8.4.170.0120.01120.59
8.4.160.0060.00320.59
8.4.150.0060.00616.92
8.4.140.0110.00917.65
8.4.130.0060.00318.67
8.4.120.0040.00420.56
8.4.110.0150.00518.54
8.4.100.0080.01317.89
8.4.90.0120.01122.54
8.4.80.0040.00718.97
8.4.70.0060.00418.91
8.4.60.0110.01220.55
8.4.50.0130.00718.58
8.4.40.0040.00717.93
8.4.30.0120.00920.65
8.4.20.0070.01320.68
8.4.10.0060.00320.50
8.3.300.0070.01319.04
8.3.290.0050.00420.92
8.3.280.0100.01218.57
8.3.270.0120.01216.79
8.3.260.0100.00916.60
8.3.250.0060.00316.54
8.3.240.0100.00616.85
8.3.230.0110.00616.65
8.3.220.0090.01020.54
8.3.210.0090.00618.37
8.3.200.0050.00416.67
8.3.190.0110.00517.18
8.3.180.0120.00618.83
8.3.170.0140.00417.30
8.3.160.0130.00618.40
8.3.150.0110.00716.73
8.3.140.0150.00416.57
8.3.130.0040.00416.61
8.3.120.0060.00318.96
8.3.110.0040.00420.94
8.3.100.0030.00624.06
8.3.90.0000.00826.77
8.3.80.0090.00016.75
8.3.70.0180.00417.00
8.3.60.0160.00318.56
8.3.50.0080.01123.69
8.3.40.0060.01519.02
8.3.30.0120.00318.55
8.3.20.0040.00424.18
8.3.10.0050.00324.66
8.3.00.0040.00426.16
8.2.300.0110.01020.43
8.2.290.0150.00517.17
8.2.280.0100.00518.28
8.2.270.0060.01216.63
8.2.260.0140.00417.05
8.2.250.0000.00816.69
8.2.240.0090.00018.95
8.2.230.0030.00622.58
8.2.220.0090.00037.54
8.2.210.0000.00826.77
8.2.200.0060.00316.50
8.2.190.0070.00716.58
8.2.180.0140.00725.92
8.2.170.0150.00019.00
8.2.160.0000.01422.96
8.2.150.0030.00625.66
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0050.00326.35
8.2.110.0000.01122.23
8.2.100.0060.00617.91
8.2.90.0060.00317.84
8.2.80.0000.00818.03
8.2.70.0080.00017.75
8.2.60.0050.00317.63
8.2.50.0040.00418.05
8.2.40.0040.00418.34
8.2.30.0000.00718.09
8.2.20.0040.00418.03
8.2.10.0050.00319.23
8.2.00.0070.00019.25
8.1.340.0120.01022.15
8.1.330.0110.00816.07
8.1.320.0120.00817.75
8.1.310.0150.00316.85
8.1.300.0120.00316.16
8.1.290.0060.00318.88
8.1.280.0070.01425.92
8.1.270.0050.00323.99
8.1.260.0050.00328.09
8.1.250.0040.00428.09
8.1.240.0060.00322.08
8.1.230.0040.00722.71
8.1.220.0040.00417.78
8.1.210.0080.00018.77
8.1.200.0040.00417.66
8.1.190.0000.00817.22
8.1.180.0060.00318.10
8.1.170.0000.00818.64
8.1.160.0000.00718.83
8.1.150.0000.00718.93
8.1.140.0000.00718.85
8.1.130.0040.00420.18
8.1.120.0080.00017.55
8.1.110.0050.00317.52
8.1.100.0000.00917.47
8.1.90.0000.00817.40
8.1.80.0000.01117.48
8.1.70.0000.00717.54
8.1.60.0030.00617.52
8.1.50.0000.00817.59
8.1.40.0070.00017.55
8.1.30.0000.00917.69
8.1.20.0090.00017.72
8.1.10.0040.00417.63
8.1.00.0030.00617.54
8.0.300.0090.00020.21
8.0.290.0050.00316.88
8.0.280.0030.00318.67
8.0.270.0030.00318.09
8.0.260.0040.00418.36
8.0.250.0070.00016.88
8.0.240.0000.00617.00
8.0.230.0030.00317.02
8.0.220.0030.00316.84
8.0.210.0000.00716.90
8.0.200.0040.00417.03
8.0.190.0050.00217.00
8.0.180.0030.00517.00
8.0.170.0030.00516.99
8.0.160.0000.00716.77
8.0.150.0000.00716.79
8.0.140.0040.00416.86
8.0.130.0060.00013.43
8.0.120.0020.00516.90
8.0.110.0070.00016.83
8.0.100.0080.00016.93
8.0.90.0000.00717.02
8.0.80.0040.01116.98
8.0.70.0050.00316.91
8.0.60.0050.00216.83
8.0.50.0040.00416.80
8.0.30.0100.01317.02
8.0.20.0120.00717.25
8.0.10.0040.00417.16
8.0.00.0140.00416.74
7.4.330.0000.00515.55
7.4.320.0000.00616.47
7.4.300.0040.00416.68
7.4.290.0000.00816.48
7.4.280.0000.00816.64
7.4.270.0030.00316.55
7.4.260.0060.00013.25
7.4.250.0030.00316.61
7.4.240.0030.00316.48
7.4.230.0030.00316.44
7.4.220.0050.00216.68
7.4.210.0100.00616.57
7.4.200.0080.00016.45
7.4.130.0070.01116.58
7.4.120.0130.00316.71
7.4.110.0070.01016.52
7.4.100.0080.00916.59
7.4.90.0070.01116.49
7.4.80.0030.01316.43
7.4.70.0200.00516.66
7.4.60.0130.00316.52
7.4.50.0130.01016.58
7.4.40.0060.01216.48
7.4.30.0110.00716.48
7.4.20.0090.00816.37
7.4.10.0110.00516.39
7.4.00.0070.01016.48
7.3.330.0060.00016.23
7.3.320.0050.00213.39
7.3.310.0030.00316.34
7.3.300.0030.00316.42
7.3.290.0040.01016.38
7.3.280.0060.01216.38
7.3.260.0120.00716.37
7.3.230.0030.01516.57
7.3.210.0090.00916.64
7.3.200.0100.00616.32
7.3.190.0100.00716.68
7.3.180.0090.00616.54
7.3.170.0100.01016.48
7.3.160.0030.01316.57
7.3.150.0070.01016.45
7.3.140.0120.00416.52
7.3.130.0100.00716.39
7.3.120.0130.00316.34
7.3.110.0100.00716.43
7.3.100.0030.01516.47
7.3.90.0090.00916.51
7.3.80.0030.01416.36
7.3.70.0120.00616.60
7.3.60.0100.00716.47
7.3.50.0100.01016.41
7.3.40.0090.00616.34
7.3.30.0070.01116.52
7.3.20.0120.00916.55
7.3.10.0160.00316.56
7.3.00.0120.00916.46
7.2.330.0090.00916.71
7.2.320.0060.01116.82
7.2.310.0160.00616.67
7.2.300.0060.01216.74
7.2.290.0110.00816.58
7.2.280.0130.00316.79
7.2.270.0060.01216.82
7.2.260.0090.00916.59
7.2.250.0090.00916.86
7.2.240.0070.01016.84
7.2.230.0110.01116.86
7.2.220.0060.01216.59
7.2.210.0120.00916.46
7.2.200.0120.00616.66
7.2.190.0090.01216.65
7.2.180.0090.00916.66
7.2.170.0110.00716.67
7.2.160.0090.00916.61
7.2.150.0090.01116.81
7.2.140.0070.01416.71
7.2.130.0110.00716.76
7.2.120.0170.00616.72
7.2.110.0080.01116.77
7.2.100.0140.00416.55
7.2.90.0110.00816.63
7.2.80.0140.00416.62
7.2.70.0190.00916.61
7.2.60.0060.01316.51
7.2.50.0150.00916.82
7.2.40.0120.00616.73
7.2.30.0080.01116.89
7.2.20.0100.01016.79
7.2.10.0110.00716.61
7.2.00.0110.00616.39

preferences:
50.7 ms | 1513 KiB | 5 Q