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('get', function ($field, ReflectionMethod $method) use (&$data, $transform) { $normalized = preg_replace_callback('/([A-Z])/', $transform, $field); $data[$normalized] = $method->invoke(null); }); return $data; } public function hydrate($object, array $data) { $transform = function ($key) { return 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); }; $this->processMethods('set', function($field, ReflectionMethod $method) use ($data, $transform) { $method->invoke(null, $data[$transform($field)]); }); } protected function processMethods($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, $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.70.0160.00018.80
8.3.60.0070.00718.56
8.3.50.0100.01121.27
8.3.40.0150.00019.17
8.3.30.0040.01119.21
8.3.20.0030.00619.11
8.3.10.0080.00020.88
8.3.00.0050.00322.07
8.2.180.0100.01016.75
8.2.170.0030.01622.96
8.2.160.0070.01020.68
8.2.150.0050.00324.18
8.2.140.0040.00424.66
8.2.130.0030.00526.16
8.2.120.0040.00420.94
8.2.110.0100.00020.35
8.2.100.0080.00418.09
8.2.90.0040.00419.30
8.2.80.0080.00017.97
8.2.70.0000.00918.13
8.2.60.0080.00018.16
8.2.50.0080.00018.07
8.2.40.0030.00618.28
8.2.30.0060.00321.32
8.2.20.0040.00417.99
8.2.10.0000.00817.97
8.2.00.0080.00017.93
8.1.280.0130.00325.92
8.1.270.0080.00023.99
8.1.260.0070.01426.35
8.1.250.0000.00828.09
8.1.240.0030.00722.50
8.1.230.0090.00321.16
8.1.220.0000.00918.77
8.1.210.0030.00619.02
8.1.200.0030.00717.60
8.1.190.0040.00419.41
8.1.180.0050.00318.10
8.1.170.0040.00418.75
8.1.160.0030.00619.02
8.1.150.0040.00418.95
8.1.140.0080.00017.70
8.1.130.0030.00517.82
8.1.120.0000.00717.74
8.1.110.0070.00017.72
8.1.100.0000.00817.63
8.1.90.0000.00717.75
8.1.80.0040.00417.70
8.1.70.0030.00317.72
8.1.60.0040.00417.82
8.1.50.0080.00017.63
8.1.40.0000.00817.71
8.1.30.0020.00517.94
8.1.20.0080.00017.81
8.1.10.0000.00817.86
8.1.00.0080.00017.64
8.0.300.0030.00620.10
8.0.290.0040.00417.29
8.0.280.0040.00418.55
8.0.270.0050.00217.29
8.0.260.0050.00517.06
8.0.250.0040.00417.16
8.0.240.0080.00017.29
8.0.230.0030.00317.32
8.0.220.0040.00417.16
8.0.210.0030.00617.27
8.0.200.0070.00017.13
8.0.190.0050.00217.25
8.0.180.0040.00417.12
8.0.170.0040.00417.15
8.0.160.0050.00317.22
8.0.150.0050.00317.15
8.0.140.0040.00417.07
8.0.130.0000.00613.62
8.0.120.0040.00417.16
8.0.110.0000.00917.05
8.0.100.0050.00317.11
8.0.90.0040.00417.16
8.0.80.0150.00817.12
8.0.70.0030.00617.14
8.0.60.0020.00517.07
8.0.50.0050.00317.19
8.0.30.0110.01017.44
8.0.20.0150.00917.49
8.0.10.0080.00017.30
8.0.00.0100.00817.16
7.4.330.0000.00515.02
7.4.320.0020.00516.82
7.4.300.0070.00016.82
7.4.290.0030.00316.72
7.4.280.0050.00316.79
7.4.270.0040.00416.71
7.4.260.0000.00613.65
7.4.250.0000.00816.68
7.4.240.0000.00716.84
7.4.230.0000.00717.02
7.4.220.0090.00916.95
7.4.210.0080.00716.88
7.4.200.0000.00716.94
7.4.190.0000.00916.72
7.4.160.0100.00816.75
7.4.150.0000.01917.40
7.4.140.0030.01517.86
7.4.130.0060.01416.84
7.4.120.0130.00616.76
7.4.110.0060.01216.79
7.4.100.0110.00816.65
7.4.90.0090.01516.81
7.4.80.0090.00919.39
7.4.70.0080.01416.66
7.4.60.0090.00916.71
7.4.50.0030.00616.57
7.4.40.0070.00722.77
7.4.30.0080.01316.92
7.4.10.0070.01015.11
7.4.00.0100.00615.28
7.3.330.0000.00513.65
7.3.320.0030.00313.52
7.3.310.0040.00416.68
7.3.300.0050.00316.70
7.3.290.0060.00916.70
7.3.280.0080.00916.71
7.3.270.0170.00617.40
7.3.260.0090.01118.24
7.3.250.0120.00716.68
7.3.240.0130.00516.89
7.3.230.0140.00316.77
7.3.210.0080.01316.88
7.3.200.0100.01419.39
7.3.190.0090.00916.77
7.3.180.0040.01216.65
7.3.170.0070.01116.71
7.3.160.0030.01516.64
7.3.130.0100.01015.34
7.3.120.0080.01015.24
7.3.110.0080.01114.96
7.3.100.0080.00615.09
7.3.90.0050.00815.22
7.3.80.0050.00915.12
7.3.70.0060.01015.16
7.3.60.0100.00215.04
7.3.50.0050.00415.13
7.3.40.0070.01015.14
7.3.30.0070.00914.95
7.3.20.0030.00916.93
7.3.10.0100.00616.91
7.3.00.0160.00616.75
7.2.330.0120.00917.14
7.2.320.0100.01417.08
7.2.310.0160.01316.94
7.2.300.0110.00616.90
7.2.290.0110.00716.77
7.2.260.0000.01815.39
7.2.250.0130.00615.36
7.2.240.0090.00915.23
7.2.230.0070.00915.28
7.2.220.0050.00915.31
7.2.210.0030.01015.40
7.2.200.0050.01015.33
7.2.190.0070.00615.34
7.2.180.0060.00415.23
7.2.170.0050.00815.41
7.2.160.0000.01415.52
7.2.150.0090.00317.09
7.2.140.0060.00616.90
7.2.130.0220.01017.01
7.2.120.0210.00817.02
7.2.110.0260.01117.03
7.2.100.0100.00717.23
7.2.90.0280.01217.21
7.2.80.0160.01016.87
7.2.70.0180.00917.17
7.2.60.0060.01117.10
7.2.50.0220.01417.00
7.2.40.0080.01017.04
7.2.30.0090.01016.81
7.2.20.0130.00617.11
7.2.10.0120.00416.91
7.2.00.0070.01117.14
7.1.330.0070.01016.04
7.1.320.0080.00716.10
7.1.310.0050.00815.90
7.1.300.0050.00916.07
7.1.290.0020.01316.04
7.1.280.0090.00716.01
7.1.270.0080.00415.73
7.1.260.0040.00915.93
7.1.250.0140.00715.80
7.1.240.0030.00616.10
7.1.230.0080.00416.18
7.1.220.0090.00616.00
7.1.210.0040.01116.03
7.1.200.0050.00815.99
7.1.190.0070.00316.18
7.1.180.0040.00715.91
7.1.170.0060.00316.07
7.1.160.0060.00316.16
7.1.150.0030.00916.16
7.1.140.0060.01016.13
7.1.130.0070.00716.15
7.1.120.0030.00516.04
7.1.110.0000.01216.08
7.1.100.0020.01117.13
7.1.90.0060.00615.76
7.1.80.0030.01415.88
7.1.70.0020.00816.58
7.1.60.0070.01417.78
7.1.50.0110.00916.41
7.1.40.0000.01516.18
7.1.30.0090.00316.05
7.1.20.0030.00616.17
7.1.10.0030.01016.04
7.1.00.0050.02019.32
7.0.330.0030.00815.82
7.0.320.0070.00715.62
7.0.310.0090.00615.41
7.0.300.0040.00915.70
7.0.290.0030.00715.65
7.0.280.0060.00615.33
7.0.270.0030.01015.53
7.0.260.0100.00315.85
7.0.250.0090.00615.45
7.0.240.0030.01115.65
7.0.230.0100.00315.47
7.0.220.0000.01015.43
7.0.210.0070.00715.71
7.0.200.0060.00616.23
7.0.190.0100.00715.65
7.0.180.0030.00915.79
7.0.170.0070.00715.58
7.0.160.0090.00615.68
7.0.150.0030.01315.45
7.0.140.0030.04018.89
7.0.130.0000.00915.73
7.0.120.0130.00015.73
7.0.110.0090.00615.43
7.0.100.0030.04717.76
7.0.90.0070.03317.79
7.0.80.0090.02617.85
7.0.70.0060.04217.86
7.0.60.0160.01717.92
7.0.50.0050.03418.04
7.0.40.0090.03817.00
7.0.30.0080.04017.01
7.0.20.0050.04417.00
7.0.10.0030.04717.01
7.0.00.0120.04116.99
5.6.400.0070.00714.65
5.6.390.0030.01014.85
5.6.380.0070.00314.75
5.6.370.0000.01514.96
5.6.360.0090.00614.65
5.6.350.0060.00914.86
5.6.340.0030.01014.90
5.6.330.0090.00614.31
5.6.320.0000.01514.33
5.6.310.0160.00014.76
5.6.300.0070.01014.29
5.6.290.0000.01214.64
5.6.280.0090.04017.88
5.6.270.0120.00414.82
5.6.260.0000.01514.77
5.6.250.0060.02217.63
5.6.240.0080.02817.61
5.6.230.0050.04317.54
5.6.220.0070.04217.70
5.6.210.0080.02717.52
5.6.200.0040.03017.88
5.6.190.0080.04617.84
5.6.180.0110.03617.96
5.6.170.0120.02517.85
5.6.160.0050.04517.77
5.6.150.0120.04317.79
5.6.140.0030.03117.83
5.6.130.0070.04117.90
5.6.120.0020.02717.87
5.6.110.0070.04217.83
5.6.100.0060.04617.96
5.6.90.0100.04517.83
5.6.80.0050.04017.53
5.6.70.0090.04417.53
5.6.60.0080.03717.53
5.6.50.0050.04217.50
5.6.40.0030.03317.63
5.6.30.0040.04417.60
5.6.20.0080.03917.53
5.6.10.0070.04417.44
5.6.00.0020.03617.43
5.5.380.0070.03917.31
5.5.370.0070.03817.55
5.5.360.0080.04517.53
5.5.350.0110.03217.56
5.5.340.0070.04417.83
5.5.330.0050.03917.70
5.5.320.0030.04617.69
5.5.310.0070.04117.79
5.5.300.0080.04217.87
5.5.290.0100.02317.83
5.5.280.0020.04717.76
5.5.270.0030.04517.67
5.5.260.0030.04317.63
5.5.250.0140.04217.62
5.5.240.0040.04517.49
5.5.230.0080.04317.43
5.5.220.0050.04817.42
5.5.210.0100.03017.50
5.5.200.0050.04617.50
5.5.190.0080.04117.43
5.5.180.0030.05117.29
5.5.170.0030.01014.34
5.5.160.0070.04617.49
5.5.150.0050.02717.27
5.5.140.0030.03717.52
5.5.130.0040.03217.26
5.5.120.0030.02917.38
5.5.110.0080.04517.46
5.5.100.0110.04117.29
5.5.90.0130.03817.42
5.5.80.0050.04517.26
5.5.70.0080.04117.36
5.5.60.0110.03817.32
5.5.50.0080.04117.34
5.5.40.0050.04517.26
5.5.30.0070.04317.19
5.5.20.0070.04017.26
5.5.10.0080.04517.36
5.5.00.0030.04617.36
5.4.450.0050.04216.59
5.4.440.0020.04316.66
5.4.430.0070.04216.63
5.4.420.0090.03816.68
5.4.410.0050.04516.59
5.4.400.0020.03716.55
5.4.390.0050.03016.46
5.4.380.0030.02716.40
5.4.370.0070.03816.47
5.4.360.0100.03716.54
5.4.350.0130.03516.54
5.4.340.0080.04216.40
5.4.330.0080.00013.89
5.4.320.0060.03116.47
5.4.310.0070.04616.51
5.4.300.0070.04016.47
5.4.290.0090.02516.48
5.4.280.0060.04216.38
5.4.270.0100.03116.37
5.4.260.0070.04216.51
5.4.250.0100.03816.38
5.4.240.0050.04016.49
5.4.230.0000.03016.52
5.4.220.0130.03616.51
5.4.210.0100.03716.49
5.4.200.0070.04016.46
5.4.190.0050.04016.52
5.4.180.0070.04416.45
5.4.170.0070.04216.42
5.4.160.0030.02816.46
5.4.150.0070.03516.40
5.4.140.0140.01915.18
5.4.130.0060.02115.16
5.4.120.0070.04315.06
5.4.110.0020.02515.19
5.4.100.0050.02915.23
5.4.90.0050.02215.14
5.4.80.0080.02315.14
5.4.70.0050.03515.22
5.4.60.0070.02215.21
5.4.50.0100.02815.21
5.4.40.0050.02215.15
5.4.30.0060.04015.15
5.4.20.0010.02715.19
5.4.10.0040.03715.12
5.4.00.0030.03114.86
5.3.290.0030.02714.37
5.3.280.0050.04314.25
5.3.270.0120.03514.32
5.3.260.0070.03714.25
5.3.250.0070.04014.25
5.3.240.0050.04414.25
5.3.230.0080.02514.29
5.3.220.0080.03814.25
5.3.210.0050.02814.24
5.3.200.0100.03514.26
5.3.190.0050.04214.24
5.3.180.0050.03214.32
5.3.170.0090.03414.27
5.3.160.0080.04014.32
5.3.150.0060.02314.26
5.3.140.0030.03314.31
5.3.130.0080.04214.26
5.3.120.0050.03514.24
5.3.110.0020.03814.23
5.3.100.0030.02513.98
5.3.90.0070.02013.96
5.3.80.0050.02313.96
5.3.70.0070.02013.99
5.3.60.0020.02314.03
5.3.50.0010.02313.96
5.3.40.0030.02313.95
5.3.30.0070.01813.97
5.3.20.0020.02413.89
5.3.10.0000.03413.86
5.3.00.0000.02313.81

preferences:
57 ms | 401 KiB | 5 Q