3v4l.org

run code in 300+ PHP versions simultaneously
<?php error_reporting(E_ALL); class DtoException extends Exception { } abstract class Dto { private static $propertyNamesByClass = array(); private function __construct(array $propertiesKeyValues) { foreach($propertiesKeyValues as $propertyName=>$propertyValue) if(Static::hasPropertyName($propertyName)) $this->set($propertyName,$propertyValue); } public function __call($method, array $arguments) { $getOrSet = substr($method, 0, 3); if($getOrSet != 'get' && $getOrSet != 'set') throw new DtoException('"'.get_class($this).'" has no method "'.$method.'"'); $propertyName = strtolower(substr($method, 3)); if(!Static::hasPropertyName($propertyName)) throw new DtoException('"'.get_class($this).'" has no property "'.$propertyName.'"'); $getOrSetMethod = array($this,$getOrSet); array_unshift($arguments,$propertyName); return call_user_func_array($getOrSetMethod, $arguments); } public function __get($propertyName) { if(!Static::hasPropertyName($propertyName)) throw new DtoException('"'.get_class($this).'" has no property "'.$propertyName.'"'); return $this->get($propertyName); } public function __set($propertyName, $propertyValue) { if(!Static::hasPropertyName($propertyName)) throw new DtoException('"'.get_class($this).'" has no property "'.$propertyName.'"'); return $this->set($propertyName, $propertyValue); } public static function createFromArray($propertiesKeyValues) { return new Static($propertiesKeyValues); } public function set($propertyName, $propertyValue) { $this->$propertyName = $propertyValue; } public function get($propertyName) { return $this->$propertyName; } public static function getPropertyNames() { $className = get_called_class(); if(isset(self::$propertyNamesByClass[$className])) return self::$propertyNamesByClass[$className]; $reflected = new ReflectionClass($className); $properties = $reflected->getProperties( ReflectionProperty::IS_PUBLIC ); $propertyNames = array(); foreach($properties as $property) { $propertyNames[] = $property->getName(); } self::$propertyNamesByClass[$className] = $propertyNames; return $propertyNames; } public static function hasPropertyName($propertyName) { $propertyNames = Static::getPropertyNames(); return in_array($propertyName,$propertyNames); } public function asArray() { $values = array(); foreach (Static::getPropertyNames() as $propertyName) { $values[$propertyName] = $this->get($propertyName); } return $values; } public function __sleep() { $propertyNames = self::getPropertyNames(); foreach ($propertyNames as $propertyName) { $propertyValue = $this->get($propertyName); $this->set($propertyName, $propertyValue); } return $propertyNames; } public function __wakeup() { $propertyNames = self::getPropertyNames(); return $propertyNames; } } class Person extends Dto { public $name; public $surname; } class Test { const PERSON_NAME = 'name'; const PERSON_SURNAME = 'surname'; public function testProvideDtoPropertyNames() { $propertyNames = Person::getPropertyNames(); $expectedProperties = array( self::PERSON_NAME, self::PERSON_SURNAME, ); $this->assertEquals( $expectedProperties, $propertyNames ); } public function testProvidePropertyViaGeneralGetter() { $dto = Person::createFromArray(array( self::PERSON_NAME => 'Simone', )); $this->assertEquals( 'Simone', $dto->getName() ); } public function testDTOAcceptOnlyItsOwnProperties() { $dto = Person::createFromArray(array( self::PERSON_NAME => 'Simone', 'non existent property' => 'Simone', )); $expectedProperties = array( self::PERSON_NAME => 'Simone', self::PERSON_SURNAME => null, ); $this->assertEquals( $expectedProperties, $dto->asArray() ); } public function testSerializationKeepSameProperties() { $properties = array( self::PERSON_NAME => 'Simone', self::PERSON_SURNAME => null, ); $dto = Person::createFromArray($properties); $serialized = serialize($dto); $unserialized = unserialize($serialized); $this->assertEquals( $dto->asArray(), $unserialized->asArray() ); $this->assertEquals( $properties, $unserialized->asArray() ); } public function assertEquals($a,$b) { $serializeA = serialize($a); $serializeB = serialize($b); echo ($serializeA==$serializeB?'Pass':'FAIL').' : '.$serializeA.' ; '.$serializeB."\n"; } } $test = new Test(); $test->testProvideDtoPropertyNames(); $test->testProvidePropertyViaGeneralGetter(); $test->testDTOAcceptOnlyItsOwnProperties(); $test->testSerializationKeepSameProperties();

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.0080.01116.75
8.3.50.0100.00618.07
8.3.40.0090.00619.14
8.3.30.0040.01218.80
8.3.20.0000.00720.21
8.3.10.0050.00321.66
8.3.00.0030.00519.38
8.2.180.0090.00618.07
8.2.170.0090.01222.96
8.2.160.0110.00420.90
8.2.150.0040.00425.66
8.2.140.0080.00024.66
8.2.130.0030.00522.21
8.2.120.0030.00626.35
8.2.110.0120.00021.11
8.2.100.0080.00417.68
8.2.90.0080.00018.03
8.2.80.0000.00818.22
8.2.70.0030.00617.63
8.2.60.0000.01117.63
8.2.50.0030.00517.63
8.2.40.0040.00419.79
8.2.30.0140.00218.47
8.2.20.0100.00317.81
8.2.10.0070.00617.82
8.2.00.0050.00618.02
8.1.280.0110.00725.92
8.1.270.0040.01223.77
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0030.00623.92
8.1.230.0000.01122.80
8.1.220.0000.00817.91
8.1.210.0040.00418.77
8.1.200.0030.00617.48
8.1.190.0040.00417.35
8.1.180.0100.00018.10
8.1.170.0060.00317.62
8.1.160.0050.00519.02
8.1.150.0100.00418.29
8.1.140.0050.00717.71
8.1.130.0070.00518.25
8.1.120.0100.00217.56
8.1.110.0090.00517.55
8.1.100.0070.00517.56
8.1.90.0060.00617.55
8.1.80.0030.01017.55
8.1.70.0070.00417.51
8.1.60.0070.00717.68
8.1.50.0050.00817.55
8.1.40.0080.00617.58
8.1.30.0070.00617.63
8.1.20.0090.00417.64
8.1.10.0110.00217.63
8.1.00.0070.00617.59
8.0.300.0030.00520.04
8.0.290.0000.00717.00
8.0.280.0050.00718.04
8.0.270.0020.01017.25
8.0.260.0050.00718.08
8.0.250.0080.00417.35
8.0.240.0070.00617.30
8.0.230.0070.00517.33
8.0.220.0080.00317.30
8.0.210.0090.00217.25
8.0.200.0120.00017.34
8.0.190.0060.00617.25
8.0.180.0110.00317.33
8.0.170.0090.00417.32
8.0.160.0090.00317.33
8.0.150.0060.00517.32
8.0.140.0100.00217.32
8.0.130.0060.00615.52
8.0.120.0070.00517.28
8.0.110.0080.00417.26
8.0.100.0060.00617.23
8.0.90.0060.00617.19
8.0.80.0100.00817.28
8.0.70.0100.00217.33
8.0.60.0080.00417.31
8.0.50.0070.00417.30
8.0.30.0070.01017.40
8.0.20.0090.00917.43
8.0.10.0050.00817.42
8.0.00.0070.01117.07
7.4.330.0070.00116.57
7.4.320.0080.00417.08
7.4.300.0080.00317.08
7.4.290.0080.00417.02
7.4.280.0060.00617.12
7.4.270.0100.00217.08
7.4.260.0110.00217.11
7.4.250.0080.00417.11
7.4.240.0030.00817.13
7.4.230.0040.00717.02
7.4.220.0070.00617.13
7.4.210.0100.00517.02
7.4.200.0060.00617.07
7.4.190.0080.00817.59
7.4.180.0130.00417.59
7.4.160.0090.00816.81
7.4.150.0120.00417.49
7.4.140.0110.01017.80
7.4.130.0080.01217.10
7.4.120.0100.01016.71
7.4.110.0090.00717.18
7.4.100.0070.01117.04
7.4.90.0040.01317.06
7.4.80.0130.00718.49
7.4.70.0090.00717.15
7.4.60.0140.00217.01
7.4.50.0100.00617.15
7.4.40.0100.00517.11
7.4.30.0080.00817.59
7.4.20.0050.00917.59
7.4.10.0080.00716.37
7.4.00.0080.00816.02
7.3.330.0040.00615.44
7.3.320.0090.00315.42
7.3.310.0060.00617.06
7.3.300.0040.00716.97
7.3.290.0090.00316.94
7.3.280.0060.00916.84
7.3.270.0120.00517.49
7.3.260.0100.00916.85
7.3.250.0080.00916.82
7.3.240.0120.00916.89
7.3.230.0110.00717.14
7.3.220.0130.00317.59
7.3.210.0100.00717.07
7.3.200.0090.00917.17
7.3.190.0150.00217.05
7.3.180.0110.00617.12
7.3.170.0110.00517.07
7.3.160.0130.00416.96
7.3.150.0120.00317.59
7.3.140.0130.00317.59
7.3.130.0070.01216.29
7.3.120.0110.00516.22
7.3.110.0120.00616.24
7.3.100.0040.01216.37
7.3.90.0090.00616.24
7.3.80.0100.00516.35
7.3.70.0050.01116.32
7.3.60.0090.00516.34
7.3.50.0070.00916.07
7.3.40.0130.00216.13
7.3.30.0050.00716.29
7.3.20.0070.00817.16
7.3.10.0080.00717.09
7.3.00.0090.00517.22
7.2.340.0050.01017.59
7.2.330.0100.00617.11
7.2.320.0150.00117.21
7.2.310.0080.00817.13
7.2.300.0070.01017.13
7.2.290.0070.00917.08
7.2.280.0150.00017.59
7.2.270.0140.00217.59
7.2.260.0050.01416.20
7.2.250.0110.00616.42
7.2.240.0110.00716.40
7.2.230.0120.00416.35
7.2.220.0110.00616.37
7.2.210.0110.00216.29
7.2.200.0090.00716.29
7.2.190.0110.00416.34
7.2.180.0090.00716.20
7.2.170.0090.00716.43
7.2.160.0050.01116.27
7.2.150.0140.00217.51
7.2.140.0080.00717.33
7.2.130.0120.00717.69
7.2.120.0070.00917.49
7.2.110.0120.00717.51
7.2.100.0150.00517.40
7.2.90.0040.01117.59
7.2.80.0070.00717.49
7.2.70.0090.00917.42
7.2.60.0080.00817.40
7.2.50.0140.00217.58
7.2.40.0110.00717.62
7.2.30.0070.00817.63
7.2.20.0070.01017.51
7.2.10.0190.00617.86
7.2.00.0180.00917.64
7.1.330.0130.00216.80
7.1.320.0080.00916.63
7.1.310.0110.00516.82
7.1.300.0090.00516.77
7.1.290.0090.00516.79
7.1.280.0110.00416.71
7.1.270.0110.00416.68
7.1.260.0050.00916.60
7.1.250.0040.01416.81
7.1.240.0070.00716.83
7.1.230.0110.00516.77
7.1.220.0070.00616.63
7.1.210.0060.00916.74
7.1.200.0080.00516.49
7.1.190.0070.00816.76
7.1.180.0090.00616.78
7.1.170.0060.00916.68
7.1.160.0090.00616.70
7.1.150.0070.00816.62
7.1.140.0090.00516.74
7.1.130.0190.00616.71
7.1.120.0150.00916.72
7.1.110.0130.00716.34
7.1.100.0140.00816.62
7.1.90.0640.00616.50
7.1.80.0100.00916.54
7.1.70.0130.01116.04
7.1.60.0230.01024.88
7.1.50.0220.00924.88
7.1.40.0190.01024.76
7.1.30.0280.01024.78
7.1.20.0290.00724.80
7.1.10.0180.00715.75
7.1.00.0130.00815.85
7.0.330.0080.00516.52
7.0.320.0060.00816.49
7.0.310.0080.00516.61
7.0.300.0100.00316.51
7.0.290.0090.00416.53
7.0.280.0110.00316.49
7.0.270.0150.01116.35
7.0.260.0110.00816.46
7.0.250.0120.00816.17
7.0.240.0150.00516.28
7.0.230.0170.00716.19
7.0.220.0150.00616.24
7.0.210.0120.00715.68
7.0.200.0160.00715.77
7.0.190.0180.00615.71
7.0.180.0100.00715.56
7.0.170.0420.01015.50
7.0.160.0160.01015.42
7.0.150.0140.00615.53
7.0.140.0130.00715.51
7.0.130.0080.00915.67
7.0.120.0150.00615.63
7.0.110.0110.00815.57
7.0.100.0110.00815.47
7.0.90.0110.01115.62
7.0.80.0130.00715.55
7.0.70.0100.00515.64
7.0.60.0050.01215.52
7.0.50.0130.00815.67
7.0.40.0130.00615.18
7.0.30.0120.00615.13
7.0.20.0130.00315.05
7.0.10.0170.00515.03
7.0.00.0190.00715.03
5.6.400.0090.00516.18
5.6.390.0040.01016.08
5.6.380.0070.00716.09
5.6.370.0070.00616.03
5.6.360.0020.01015.92
5.6.350.0110.00316.13
5.6.340.0060.00616.16
5.6.330.0090.00316.04
5.6.320.0070.00615.98
5.6.310.0100.00516.15
5.6.300.0110.03118.52
5.6.290.0110.03218.41
5.6.280.0110.03518.56
5.6.270.0100.03218.49
5.6.260.0070.03218.42
5.6.250.0130.02918.48
5.6.240.0120.03618.46
5.6.230.0100.03318.52
5.6.220.0110.03318.42
5.6.210.0110.03318.39
5.6.200.0090.03418.39
5.6.190.0080.03618.46
5.6.180.0080.03318.41
5.6.170.0120.02918.53
5.6.160.0100.03818.45
5.6.150.0100.03818.48
5.6.140.0100.03418.52
5.6.130.0060.03618.46
5.6.120.0090.03318.49
5.6.110.0110.03218.56
5.6.100.0100.03418.42
5.6.90.0110.03118.46
5.6.80.0150.03018.10
5.6.70.0110.03217.99
5.6.60.0090.03318.13
5.6.50.0080.02918.11
5.6.40.0050.03618.02
5.6.30.0050.03618.05
5.6.20.0070.03818.11
5.6.10.0120.02818.12
5.6.00.0140.02918.13

preferences:
31.58 ms | 400 KiB | 5 Q