3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Book { protected $title; protected $author; protected $genres; public function __construct() { $this->genres = new ArrayObject(); } public function addGenre($genre) { $this->genres[] = $genre; return $this; } public function getGenres() { return $this->genres; } public function setAuthor(Person $author) { $this->author = $author; return $this; } public function getAuthor() { return $this->author; } public function setTitle($title) { $this->title = $title; return $this; } public function getTitle() { return $this->title; } } class Person { protected $firstName; protected $lastName; public function __construct($firstName = null, $lastName = null) { $this->setFirstName($firstName); $this->setLastName($lastName); } public function setFirstName($firstName) { $this->firstName = $firstName; return $this; } public function getFirstName() { return $this->firstName; } public function setLastName($lastName) { $this->lastName = $lastName; return $this; } public function getLastName() { return $this->lastName; } } interface Hydrator { public function hydrate($object, array $data); public function extract($object); } class ReflectionHydrator implements Hydrator { public function hydrate($object, array $data) { $reflection = new ReflectionClass($object); $transform = function ($key) { return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); }; foreach ($reflection->getMethod() as $method) { if (!$method->isPublic() || 0 !== strpos($method->getName(), 'set')) { continue; } $field = lcfirst(substr($method->getName(), 3)); // @todo } } public function extract($object) { $data = array(); $reflection = new ReflectionClass($object); $transform = function ($key) { return '_' . strtolower(array_shift($key)); }; foreach ($reflection->getMethods() as $method) { if (!$method->isPublic() || 0 !== strpos($method->getName(), 'get')) { continue; } $field = lcfirst(substr($method->getName(), 3)); $normalized = preg_replace_callback('/([A-Z])/', $transform, $field); $data[$normalized] = $method->invoke($object); } return $data; } } class DryReflectionHydrator implements Hydrator { public function extract($object) { $transform = function ($key) { return '_' . strtolower(array_shift($key)); }; $data = array(); // Process get methods $this->processMethods($object, 'get', function ($field, ReflectionClass $ref, ReflectionMethod $method) use (&$data, $transform) { $normalized = preg_replace_callback('/([A-Z])/', $transform, $field); $data[$normalized] = $method->invoke($ref); }); return $data; } public function hydrate($object, array $data) { $transform = function ($key) { return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); }; $this->processMethods($object, 'set', function($field, ReflectionClass $ref, ReflectionMethod $method) use ($data, $transform) { $method->invoke($ref, $data[$transform($field)]); }); } protected function processMethods($object, $prefix, $callback) { $reflection = new ReflectionClass($object); foreach ($reflection->getMethods() as $method) { if (!$method->isPublic() || 0 !== strpos($method->getName(), $prefix)) { continue; } $field = lcfirst(substr($method->getName(), strlen($prefix))); $callback($field, $reflection, $method); } } } $book = new Book(); $book->setTitle('Harry potter'); $book->addGenre('Drama')->addGenre('Nosense'); $book->setAuthor(new Person('James', 'Bond')); $hydrator = new ReflectionHydrator(); var_dump($hydrator->extract($book)); var_dump($hydrator->extract($book->getAuthor())); $dryHydrator = new DryReflectionHydrator(); var_dump($dryHydrator->extract($book)); var_dump($dryHydrator->extract($book->getAuthor()));

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.0000.01517.00
8.3.50.0100.00618.35
8.3.40.0130.00619.08
8.3.30.0120.00318.86
8.3.20.0090.00919.15
8.3.10.0000.01018.94
8.3.00.0000.00819.63
8.2.180.0180.00317.00
8.2.170.0060.01622.96
8.2.160.0040.01120.50
8.2.150.0040.00424.18
8.2.140.0040.00424.66
8.2.130.0000.00826.16
8.2.120.0000.00821.18
8.2.110.0030.00719.51
8.2.100.0110.00018.09
8.2.90.0060.00319.48
8.2.80.0040.00419.45
8.2.70.0000.00818.00
8.2.60.0030.00618.18
8.2.50.0050.00318.13
8.2.40.0060.00318.41
8.2.30.0080.00018.30
8.2.20.0000.01019.59
8.2.10.0040.00418.00
8.2.00.0000.01017.94
8.1.270.0060.00324.66
8.1.260.0040.00426.35
8.1.250.0040.00428.09
8.1.240.0000.01122.08
8.1.230.0120.00020.87
8.1.220.0000.00918.77
8.1.210.0050.00319.18
8.1.200.0080.00417.47
8.1.190.0000.00817.77
8.1.180.0000.00818.10
8.1.170.0030.00619.09
8.1.160.0040.00418.92
8.1.150.0040.00418.80
8.1.140.0040.00417.61
8.1.130.0040.00417.89
8.1.120.0040.00417.70
8.1.110.0040.00417.67
8.1.100.0070.00017.75
8.1.90.0030.00617.68
8.1.80.0000.00817.73
8.1.70.0000.00917.64
8.1.60.0090.00017.81
8.1.50.0090.00017.74
8.1.40.0040.00417.66
8.1.30.0030.00717.91
8.1.20.0040.00417.93
8.1.10.0000.00817.82
8.1.00.0050.00317.66
8.0.300.0040.00420.06
8.0.290.0040.00417.00
8.0.280.0000.00718.63
8.0.270.0040.00417.39
8.0.260.0000.00817.13
8.0.250.0040.00417.20
8.0.240.0000.00817.25
8.0.230.0050.00317.25
8.0.220.0000.00717.19
8.0.210.0070.00317.11
8.0.200.0040.00417.19
8.0.190.0030.00617.28
8.0.180.0040.00417.22
8.0.170.0040.00417.10
8.0.160.0040.00417.08
8.0.150.0040.00417.06
8.0.140.0120.00417.22
8.0.130.0000.00613.55
8.0.120.0070.00217.20
8.0.110.0000.00817.08
8.0.100.0000.00817.04
8.0.90.0070.00017.16
8.0.80.0100.01417.17
8.0.70.0050.00317.10
8.0.60.0000.00717.06
8.0.50.0000.00817.27
8.0.30.0140.00617.44
8.0.20.0140.00817.46
8.0.10.0000.00817.11
8.0.00.0140.00917.04
7.4.330.0030.00313.22
7.4.320.0070.00016.93
7.4.300.0000.00716.96
7.4.290.0030.00316.77
7.4.280.0030.00616.79
7.4.270.0000.00716.90
7.4.260.0060.00013.58
7.4.250.0030.00616.89
7.4.240.0040.00416.77
7.4.230.0000.00716.91
7.4.220.0100.00916.91
7.4.210.0030.01817.02
7.4.200.0000.00716.89
7.4.190.0040.00416.72
7.4.160.0080.01016.86
7.4.150.0100.01316.92
7.4.140.0050.01916.70
7.4.130.0090.00916.86
7.4.120.0060.01216.96
7.4.110.0150.00916.92
7.4.100.0150.00416.77
7.4.90.0100.01316.75
7.4.80.0100.01019.39
7.4.70.0150.00416.96
7.4.60.0070.01016.80
7.4.50.0040.00416.64
7.4.40.0070.00716.58
7.4.30.0090.00916.77
7.4.10.0070.01115.17
7.4.00.0070.00915.44
7.3.330.0030.00313.55
7.3.320.0070.00013.59
7.3.310.0040.00416.71
7.3.300.0030.00316.50
7.3.290.0090.00816.64
7.3.280.0090.00616.69
7.3.270.0090.00916.79
7.3.260.0140.00717.06
7.3.250.0100.01016.92
7.3.240.0130.00916.91
7.3.230.0070.01116.75
7.3.210.0120.00716.96
7.3.200.0030.01316.68
7.3.190.0120.01216.76
7.3.180.0130.00316.72
7.3.170.0090.00916.75
7.3.160.0070.01016.78
7.3.130.0140.00315.46
7.3.120.0090.00815.19
7.3.110.0050.01415.34
7.3.100.0070.00815.25
7.3.90.0070.01015.13
7.3.80.0040.00915.26
7.3.70.0090.00515.12
7.3.60.0050.01015.01
7.3.50.0090.00715.16
7.3.40.0040.00714.96
7.3.30.0040.00815.10
7.3.20.0060.00616.91
7.3.10.0080.00616.86
7.3.00.0070.00716.96
7.2.330.0120.00617.00
7.2.320.0180.00716.93
7.2.310.0160.01016.91
7.2.300.0040.01416.93
7.2.290.0110.01416.90
7.2.260.0090.00915.57
7.2.250.0070.01115.25
7.2.240.0020.01515.37
7.2.230.0080.00615.35
7.2.220.0030.01415.33
7.2.210.0080.00915.38
7.2.200.0060.00615.26
7.2.190.0030.01015.36
7.2.180.0050.00915.30
7.2.170.0040.01115.42
7.2.160.0090.00715.37
7.2.150.0130.00317.14
7.2.140.0070.00917.04
7.2.130.0080.00517.23
7.2.120.0050.00817.00
7.2.110.0060.00916.99
7.2.100.0070.00717.01
7.2.90.0080.00617.12
7.2.80.0030.01217.23
7.2.70.0050.01016.98
7.2.60.0040.01217.21
7.2.50.0040.01217.15
7.2.40.0100.00517.08
7.2.30.0020.01317.25
7.2.20.0070.00917.06
7.2.10.0100.00517.04
7.2.00.0030.01217.04
7.1.330.0100.00515.94
7.1.320.0030.01016.02
7.1.310.0030.00815.99
7.1.300.0030.01116.06
7.1.290.0060.00815.98
7.1.280.0080.00515.86
7.1.270.0060.00916.04
7.1.260.0060.01015.89
7.1.250.0030.01215.89
7.1.240.0030.01115.96
7.1.230.0050.00816.07
7.1.220.0030.01016.00
7.1.210.0090.00516.13
7.1.200.0040.00715.99
7.1.190.0050.00916.02
7.1.180.0050.00816.09
7.1.170.0050.01015.91
7.1.160.0060.00616.05
7.1.150.0070.00415.86
7.1.140.0040.01016.00
7.1.130.0080.00515.90
7.1.120.0060.00815.84
7.1.110.0060.00816.08
7.1.100.0060.00515.86
7.1.90.0050.00715.99
7.1.80.0030.01115.77
7.1.70.0030.00916.32
7.1.60.0050.01217.10
7.1.50.0030.00816.50
7.1.40.0040.00816.00
7.1.30.0040.01015.96
7.1.20.0050.00815.96
7.1.10.0050.00515.90
7.1.00.0120.02218.06
7.0.330.0090.00515.70
7.0.320.0050.00815.70
7.0.310.0030.00815.61
7.0.300.0070.00515.63
7.0.290.0080.00315.55
7.0.280.0070.00515.59
7.0.270.0060.00615.57
7.0.260.0050.00915.64
7.0.250.0050.00915.66
7.0.240.0060.00515.38
7.0.230.0050.00915.61
7.0.220.0060.00815.61
7.0.210.0060.00515.71
7.0.200.0040.00616.07
7.0.190.0090.00515.54
7.0.180.0050.00715.56
7.0.170.0000.01215.55
7.0.160.0020.00915.55
7.0.150.0080.00615.54
7.0.140.0080.02617.79
7.0.130.0080.00515.67
7.0.120.0060.03117.85
7.0.110.0080.00515.61
7.0.100.0060.02717.08
7.0.90.0050.02417.08
7.0.80.0090.02716.98
7.0.70.0100.02817.09
7.0.60.0090.02917.17
7.0.50.0060.01917.33
7.0.40.0050.03415.76
7.0.30.0060.02615.72
7.0.20.0110.02715.88
7.0.10.0090.03115.63
7.0.00.0050.03215.93
5.6.400.0060.00714.59
5.6.390.0030.01014.41
5.6.380.0090.00614.49
5.6.370.0040.01114.64
5.6.360.0020.00914.71
5.6.350.0050.01014.60
5.6.340.0030.00814.56
5.6.330.0080.00514.44
5.6.320.0040.01014.64
5.6.310.0070.00914.63
5.6.300.0070.00714.75
5.6.290.0020.01214.56
5.6.280.0020.03316.75
5.6.270.0080.00514.42
5.6.260.0030.00814.71
5.6.250.0110.01416.74
5.6.240.0090.03016.61
5.6.230.0030.03616.58
5.6.220.0090.03016.71
5.6.210.0100.03016.76
5.6.200.0020.03616.85
5.6.190.0060.02716.69
5.6.180.0070.03216.83
5.6.170.0070.03616.69
5.6.160.0080.02216.72
5.6.150.0080.03116.92
5.6.140.0060.03116.70
5.6.130.0030.03216.88
5.6.120.0130.03016.88
5.6.110.0050.03316.73
5.6.100.0030.02116.78
5.6.90.0080.03016.66
5.6.80.0070.02616.59
5.6.70.0080.03416.54
5.6.60.0080.02816.52
5.6.50.0060.01916.44
5.6.40.0060.03216.33
5.6.30.0060.03216.55
5.6.20.0080.02816.54
5.6.10.0080.02816.46
5.6.00.0050.03616.61
5.5.380.0050.02016.64
5.5.370.0040.03516.57
5.5.360.0100.03016.49
5.5.350.0070.02116.55
5.5.340.0070.02816.65
5.5.330.0080.02916.69
5.5.320.0060.03016.77
5.5.310.0090.02716.76
5.5.300.0010.03816.74
5.5.290.0070.02316.76
5.5.280.0070.03316.68
5.5.270.0070.03316.56
5.5.260.0110.02816.79
5.5.250.0080.02116.60
5.5.240.0110.02616.29
5.5.230.0060.02916.33
5.5.220.0070.03516.33
5.5.210.0040.03416.50
5.5.200.0100.02216.50
5.5.190.0120.02716.33
5.5.180.0070.02116.32
5.5.170.0050.00914.56
5.5.160.0050.03216.40
5.5.150.0120.02516.45
5.5.140.0100.02216.43
5.5.130.0090.02816.55
5.5.120.0080.02516.30
5.5.110.0080.02816.37
5.5.100.0060.03016.51
5.5.90.0070.03316.37
5.5.80.0090.02616.39
5.5.70.0070.02316.38
5.5.60.0060.03216.34
5.5.50.0090.02916.49
5.5.40.0070.02616.30
5.5.30.0070.02116.25
5.5.20.0080.02316.38
5.5.10.0060.03216.42
5.5.00.0100.01616.47
5.4.450.0070.02814.52
5.4.440.0020.03614.47
5.4.430.0060.03314.59
5.4.420.0060.01914.54
5.4.410.0060.03014.50
5.4.400.0030.02914.33
5.4.390.0070.02714.37
5.4.380.0110.02214.41
5.4.370.0060.02614.32
5.4.360.0090.02714.45
5.4.350.0030.03114.43
5.4.340.0080.02614.45
5.4.330.0030.00812.11
5.4.320.0040.02614.44
5.4.310.0070.03114.40
5.4.300.0020.03114.32
5.4.290.0020.03214.36
5.4.280.0040.03214.43
5.4.270.0050.02414.36
5.4.260.0070.02614.31
5.4.250.0020.03614.39
5.4.240.0060.02814.39
5.4.230.0020.03514.36
5.4.220.0120.02414.40
5.4.210.0060.03214.36
5.4.200.0020.03214.41
5.4.190.0050.03214.42
5.4.180.0090.02814.31
5.4.170.0080.02614.36
5.4.160.0030.02914.37
5.4.150.0090.02714.33
5.4.140.0040.02913.53
5.4.130.0070.02613.54
5.4.120.0060.02113.56
5.4.110.0050.02313.53
5.4.100.0040.02913.48
5.4.90.0070.02113.58
5.4.80.0070.01713.52
5.4.70.0080.02713.59
5.4.60.0040.03013.49
5.4.50.0060.02413.55
5.4.40.0030.02113.49
5.4.30.0110.02413.56
5.4.20.0020.03213.52
5.4.10.0100.02513.52
5.4.00.0040.03013.35
5.3.290.0090.03012.97
5.3.280.0100.02912.96
5.3.270.0060.02912.90
5.3.260.0070.03012.91
5.3.250.0100.02712.92
5.3.240.0060.02312.90
5.3.230.0080.02712.91
5.3.220.0050.02012.93
5.3.210.0110.01612.93
5.3.200.0060.02612.91
5.3.190.0050.02912.94
5.3.180.0060.03112.88
5.3.170.0070.02512.94
5.3.160.0110.01612.92
5.3.150.0060.02812.94
5.3.140.0060.02712.93
5.3.130.0040.03112.89
5.3.120.0070.03012.90
5.3.110.0100.02612.87
5.3.100.0060.02112.72
5.3.90.0080.02812.73
5.3.80.0060.02812.74
5.3.70.0070.02212.72
5.3.60.0040.02512.69
5.3.50.0060.03012.66
5.3.40.0010.02312.73
5.3.30.0050.02812.65
5.3.20.0050.02312.64
5.3.10.0050.02812.63
5.3.00.0050.02512.61

preferences:
61.77 ms | 400 KiB | 5 Q