3v4l.org

run code in 300+ PHP versions simultaneously
<?php try { $user = new User_DTO(new Validator, [ '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(new Validator, [ '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(); } class Validator { public function validate_data_against_schema( array $data, array $schema ) { foreach ( $schema as $key => $requiredType ) { if ( ! array_key_exists( $key, $data ) ) { throw new \UnexpectedValueException( sprintf( 'Required key "%s" does not exist.', $key ) ); } if ( is_array($requiredType) ) { if ( ! 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. $this->validate_data_against_schema( $data[ $key ], $requiredType ); } else { if ( gettype( $data[ $key ] ) !== $requiredType ) { throw new \UnexpectedValueException( sprintf( 'Key "%s" must be of type %s, %s provided.', $key, $requiredType, gettype( $data[ $key ] ) ) ); } } } } } abstract class DTO { abstract protected function getSchema(): array; abstract protected function initaliseFromArray($array): void; final public function __construct( Validator $validator, array $data ) { $validator->validate_data_against_schema( $data, $this->getSchema() ); } } class User_DTO extends DTO { private int $id; private string $name; private string $username; protected function getSchema(): array { return [ 'id' => 'integer', 'name' => 'string', 'username' => 'string', 'email' => 'string', 'address' => [ 'street' => 'string', 'suite' => 'string', 'city' => 'string', 'zipcode' => 'string', 'geo' => [ 'lat' => 'float', 'lng' => 'float', ], ], 'phone' => 'string', 'website' => 'string', 'company' => [ 'name' => 'string', 'catchPhrase' => 'string', 'bs' => 'string', ], ]; } protected function initaliseFromArray($array): void { $this->id = (int) $this->data['id']; $this->name = (string) $this->data['name']; $this->username = (string) $this->data['username']; } 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 } class User_List_DTO extends DTO { protected function getSchema(): array { return [ 'id' => 'integer', 'name' => 'string', 'username' => 'string', ]; } protected function initaliseFromArray($array): void { $this->id = (int) $this->data['id']; $this->name = (string) $this->data['name']; $this->username = (string) $this->data['username']; } public function get_id(): int { return $this->id; } public function get_name(): string { return $this->name; } public function get_username(): string { return $this->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.0100.01018.56
8.3.50.0110.00416.64
8.3.40.0070.00718.85
8.3.30.0130.00318.79
8.3.20.0040.00424.18
8.3.10.0040.00424.66
8.3.00.0000.00826.16
8.2.180.0080.01125.92
8.2.170.0030.01218.99
8.2.160.0030.01022.96
8.2.150.0000.00825.66
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0050.00326.35
8.2.110.0100.00722.29
8.2.100.0040.00717.84
8.2.90.0040.00717.75
8.2.80.0000.00817.97
8.2.70.0050.00517.63
8.2.60.0030.00517.48
8.2.50.0000.00918.05
8.2.40.0050.00318.22
8.2.30.0000.00818.21
8.2.20.0000.00718.16
8.2.10.0040.00419.40
8.2.00.0080.00019.24
8.1.280.0040.01125.92
8.1.270.0080.00023.99
8.1.260.0000.00728.09
8.1.250.0040.00428.09
8.1.240.0030.00622.02
8.1.230.0060.00622.68
8.1.220.0050.00317.74
8.1.210.0040.00418.77
8.1.200.0130.00017.36
8.1.190.0000.00817.35
8.1.180.0060.00318.10
8.1.170.0000.00818.50
8.1.160.0000.00718.93
8.1.150.0040.00418.92
8.1.140.0040.00418.84
8.1.130.0070.00020.22
8.1.120.0040.00417.52
8.1.110.0040.00417.43
8.1.100.0030.00617.39
8.1.90.0000.00817.40
8.1.80.0130.00017.44
8.1.70.0040.00417.38
8.1.60.0080.00417.43
8.1.50.0030.00517.50
8.1.40.0000.00817.57
8.1.30.0040.00417.65
8.1.20.0040.00417.62
8.1.10.0000.00917.59
8.1.00.0000.00817.54
8.0.300.0000.00719.71
8.0.290.0070.00316.75
8.0.280.0030.00318.61
8.0.270.0050.00217.98
8.0.260.0050.00318.49
8.0.250.0000.00717.09
8.0.240.0000.00716.95
8.0.230.0000.00717.05
8.0.220.0000.00717.01
8.0.210.0030.00317.01
8.0.200.0040.00416.95
8.0.190.0080.00016.92
8.0.180.0000.00716.97
8.0.170.0040.00416.95
8.0.160.0030.00617.02
8.0.150.0040.00416.92
8.0.140.0040.00416.84
8.0.130.0060.00013.46
8.0.120.0000.00916.99
8.0.110.0080.00017.00
8.0.100.0000.00716.82
8.0.90.0050.00216.88
8.0.80.0030.01316.95
8.0.70.0040.00416.86
8.0.60.0000.00716.88
8.0.50.0000.00816.79
8.0.30.0100.01317.06
8.0.20.0070.01116.97
8.0.10.0000.00716.98
8.0.00.0120.01816.74
7.4.330.0000.00515.55
7.4.320.0000.00716.65
7.4.300.0030.00316.63
7.4.290.0030.00516.66
7.4.280.0000.00716.55
7.4.270.0080.00016.59
7.4.260.0030.00313.26
7.4.250.0030.00316.61
7.4.240.0000.00716.48
7.4.230.0030.00316.64
7.4.220.0000.00716.70
7.4.210.0070.00916.62
7.4.200.0000.00716.43
7.4.130.0070.01116.56
7.4.120.0090.01216.45
7.4.110.0070.01016.58
7.4.100.0100.00716.73
7.4.90.0120.00916.52
7.4.80.0070.01116.53
7.4.70.0200.00016.59
7.4.60.0070.01316.37
7.4.50.0150.00616.52
7.4.40.0140.00716.73
7.4.30.0060.01216.68
7.4.20.0140.00316.43
7.4.10.0100.00716.56
7.4.00.0060.01216.66
7.3.330.0000.00816.10
7.3.320.0030.00312.98
7.3.310.0040.00416.24
7.3.300.0000.00716.20
7.3.290.0070.00916.16
7.3.280.0160.00316.16
7.3.260.0100.00916.19
7.3.230.0170.00016.25
7.3.210.0100.00716.25
7.3.200.0070.01316.30
7.3.190.0140.01016.44
7.3.180.0050.01216.24
7.3.170.0070.01316.24
7.3.160.0180.00416.30
7.3.150.0060.01616.28
7.3.140.0120.00416.18
7.3.130.0100.01016.15
7.3.120.0130.01016.13
7.3.110.0100.01216.28
7.3.100.0140.00316.32
7.3.90.0130.00316.39
7.3.80.0090.01216.24
7.3.70.0140.00716.13
7.3.60.0100.00716.12
7.3.50.0130.00716.21
7.3.40.0030.01916.21
7.3.30.0030.01416.23
7.3.20.0110.00716.19
7.3.10.0150.00416.15
7.3.00.0030.01816.16
7.2.330.0150.00616.52
7.2.320.0110.00716.15
7.2.310.0120.00616.52
7.2.300.0150.00316.47
7.2.290.0180.00316.51
7.2.280.0140.00916.56
7.2.270.0070.01116.31
7.2.260.0120.00916.43
7.2.250.0120.00616.55
7.2.240.0110.00616.29
7.2.230.0100.01416.42
7.2.220.0060.01116.46
7.2.210.0070.01516.48
7.2.200.0090.00916.45
7.2.190.0130.01016.14
7.2.180.0120.00916.29
7.2.170.0030.01916.39
7.2.160.0110.01116.41
7.2.150.0090.01216.39
7.2.140.0150.00316.52
7.2.130.0130.00716.48
7.2.120.0110.00716.48
7.2.110.0080.01116.40
7.2.100.0030.01516.21
7.2.90.0100.01416.49
7.2.80.0060.01316.55
7.2.70.0030.01516.52
7.2.60.0130.00616.16
7.2.50.0130.00616.46
7.2.40.0060.01516.38
7.2.30.0130.00916.41
7.2.20.0120.00616.45
7.2.10.0130.01616.37
7.2.00.0150.00816.39

preferences:
34.46 ms | 400 KiB | 5 Q