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()));

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.0160.00618.50
8.3.50.0050.01321.45
8.3.40.0070.01019.17
8.3.30.0120.00419.04
8.3.20.0030.00518.98
8.3.10.0090.00020.65
8.3.00.0040.00419.50
8.2.180.0070.00716.88
8.2.170.0120.00322.96
8.2.160.0160.00322.16
8.2.150.0060.00324.18
8.2.140.0040.00424.66
8.2.130.0040.00426.16
8.2.120.0050.00319.95
8.2.110.0040.01621.30
8.2.100.0070.00417.97
8.2.90.0040.00419.30
8.2.80.0050.00318.16
8.2.70.0000.00818.00
8.2.60.0000.00818.04
8.2.50.0040.00418.16
8.2.40.0040.00418.33
8.2.30.0040.00419.92
8.2.20.0030.00517.88
8.2.10.0030.00517.99
8.2.00.0030.00517.90
8.1.280.0080.00825.92
8.1.270.0080.00023.99
8.1.260.0080.00026.35
8.1.250.0000.01128.09
8.1.240.0040.00820.92
8.1.230.0070.00420.99
8.1.220.0070.00318.77
8.1.210.0040.00418.97
8.1.200.0070.00317.60
8.1.190.0000.00817.55
8.1.180.0040.00418.10
8.1.170.0000.00818.96
8.1.160.0040.00418.93
8.1.150.0000.00818.93
8.1.140.0030.00517.66
8.1.130.0000.00717.95
8.1.120.0040.00417.75
8.1.110.0040.00417.76
8.1.100.0080.00017.63
8.1.90.0040.00417.62
8.1.80.0000.00717.75
8.1.70.0000.00717.70
8.1.60.0030.00517.83
8.1.50.0000.00817.63
8.1.40.0030.00617.73
8.1.30.0060.00317.73
8.1.20.0040.00417.91
8.1.10.0040.00417.87
8.1.00.0040.00417.73
8.0.300.0030.00520.12
8.0.290.0090.00016.88
8.0.280.0030.00318.56
8.0.270.0050.00317.27
8.0.260.0000.00817.04
8.0.250.0000.00817.17
8.0.240.0050.00317.18
8.0.230.0040.00417.30
8.0.220.0040.00417.11
8.0.210.0080.00017.21
8.0.200.0040.00417.32
8.0.190.0000.00717.19
8.0.180.0000.00817.12
8.0.170.0000.00917.16
8.0.160.0070.00017.29
8.0.150.0090.00017.10
8.0.140.0030.00517.05
8.0.130.0060.00013.64
8.0.120.0000.00817.17
8.0.110.0000.00817.23
8.0.100.0070.00017.11
8.0.90.0000.00717.02
8.0.80.0070.01017.20
8.0.70.0040.00417.09
8.0.60.0000.00817.10
8.0.50.0080.00017.23
8.0.30.0130.00917.32
8.0.20.0140.00717.55
8.0.10.0040.00417.15
8.0.00.0130.00917.09
7.4.330.0030.00314.95
7.4.320.0000.00616.72
7.4.300.0000.00616.92
7.4.290.0050.00316.86
7.4.280.0040.00416.81
7.4.270.0000.00716.81
7.4.260.0000.00816.70
7.4.250.0000.00916.75
7.4.240.0000.00716.80
7.4.230.0040.00416.91
7.4.220.0130.00616.78
7.4.210.0080.00816.86
7.4.200.0040.00416.76
7.4.190.0040.00416.96
7.4.160.0130.01016.91
7.4.150.0140.00417.40
7.4.140.0140.01617.86
7.4.130.0070.01116.84
7.4.120.0090.01216.76
7.4.110.0140.00416.84
7.4.100.0100.01016.82
7.4.90.0090.00916.99
7.4.80.0100.00719.04
7.4.70.0080.00816.75
7.4.60.0070.01116.89
7.4.50.0000.00516.73
7.4.40.0120.00622.27
7.4.30.0000.01616.84
7.4.00.0030.01315.08
7.3.330.0000.00613.59
7.3.320.0030.00313.54
7.3.310.0040.00416.64
7.3.300.0050.00216.50
7.3.290.0040.01116.69
7.3.280.0110.00916.65
7.3.270.0130.01017.40
7.3.260.0120.00918.24
7.3.250.0100.01116.71
7.3.240.0060.01316.90
7.3.230.0030.01816.69
7.3.210.0140.00816.95
7.3.200.0120.00619.39
7.3.190.0040.01316.67
7.3.180.0110.00716.50
7.3.170.0090.00916.71
7.3.160.0180.00316.46
7.3.120.0040.01115.29
7.2.330.0030.01616.99
7.2.320.0090.00916.97
7.2.310.0120.01216.99
7.2.300.0070.01016.93
7.2.290.0030.01316.96
7.1.70.0090.00617.34
7.1.60.0030.01019.40
7.1.50.0180.00317.08
7.1.00.0100.06322.42
7.0.200.0000.01016.75
7.0.140.0000.04321.92
7.0.100.0130.03720.06
7.0.90.0130.06720.00
7.0.80.0070.05719.98
7.0.70.0070.08020.05
7.0.60.0070.04320.11
7.0.50.0070.03720.45
7.0.40.0030.04719.96
7.0.30.0070.04319.95
7.0.20.0100.03720.17
7.0.10.0070.07320.08
7.0.00.0030.04319.99
5.6.280.0100.07021.17
5.6.250.0100.03720.64
5.6.240.0170.03020.79
5.6.230.0070.08720.68
5.6.220.0030.03720.79
5.6.210.0070.07320.70
5.6.200.0030.04021.14
5.6.190.0000.04321.10
5.6.180.0030.04021.14
5.6.170.0070.05321.14
5.6.160.0070.04021.07
5.6.150.0030.04021.10
5.6.140.0100.04021.15
5.6.130.0070.03721.23
5.6.120.0030.06021.10
5.6.110.0030.04321.10
5.6.100.0130.06721.17
5.6.90.0030.04020.99
5.6.80.0030.05320.49
5.6.70.0100.04020.54
5.6.60.0030.04020.43
5.6.50.0000.04320.48
5.6.40.0070.04020.58
5.6.30.0070.06020.39
5.6.20.0070.08320.43
5.6.10.0200.06720.44
5.6.00.0000.08320.33
5.5.380.0030.04320.46
5.5.370.0030.04020.44
5.5.360.0030.05320.39
5.5.350.0070.03320.50
5.5.340.0070.03720.79
5.5.330.0100.03320.86
5.5.320.0030.04020.85
5.5.310.0030.04020.91
5.5.300.0100.03720.81
5.5.290.0070.03720.84
5.5.280.0030.04320.80
5.5.270.0070.04020.96
5.5.260.0030.05720.87
5.5.250.0030.04720.79
5.5.240.0000.06320.07
5.5.230.0000.06320.33
5.5.220.0000.06020.31
5.5.210.0100.04020.26
5.5.200.0070.03320.23
5.5.190.0070.05320.23
5.5.180.0100.07020.24
5.5.160.0030.03720.14
5.5.150.0070.06720.13
5.5.140.0030.07320.27
5.5.130.0030.08320.32
5.5.120.0130.04020.16
5.5.110.0170.06020.23
5.5.100.0100.06320.15
5.5.90.0170.04320.12
5.5.80.0130.06720.20
5.5.70.0170.07020.11
5.5.60.0030.08320.13
5.5.50.0100.05320.12
5.5.40.0130.07020.08
5.5.30.0100.05020.10
5.5.20.0130.07020.03
5.5.10.0100.08020.02
5.5.00.0200.06020.02
5.4.450.0070.03719.21
5.4.440.0070.03319.46
5.4.430.0000.04319.55
5.4.420.0030.04019.41
5.4.410.0070.03719.36
5.4.400.0000.03719.16
5.4.390.0100.03018.91
5.4.380.0030.03719.12
5.4.370.0130.02719.10
5.4.360.0070.03719.05
5.4.350.0030.06019.14
5.4.340.0070.04019.14
5.4.320.0030.07718.86
5.4.310.0030.07718.85
5.4.300.0130.06719.08
5.4.290.0030.08019.11
5.4.280.0100.04319.04
5.4.270.0100.05019.13
5.4.260.0100.07319.12
5.4.250.0130.04319.12
5.4.240.0100.03319.02
5.4.230.0070.07718.84
5.4.220.0200.06319.15
5.4.210.0130.07018.84
5.4.200.0100.07719.02
5.4.190.0100.07319.16
5.4.180.0070.06319.07
5.4.170.0130.06719.13
5.4.160.0170.03319.14
5.4.150.0030.04719.08
5.4.140.0070.04316.19
5.4.130.0070.07716.41
5.4.120.0030.07316.51
5.4.110.0100.06716.48
5.4.100.0100.06016.54
5.4.90.0070.06316.54
5.4.80.0100.07016.43
5.4.70.0170.06716.46
5.4.60.0100.06016.30
5.4.50.0030.07716.46
5.4.40.0070.07716.46
5.4.30.0100.03716.29
5.4.20.0130.05716.49
5.4.10.0000.07316.36
5.4.00.0070.06015.91
5.3.290.0070.03314.73
5.3.280.0070.04714.64
5.3.270.0070.07014.67
5.3.260.0070.08014.75
5.3.250.0070.06314.82
5.3.240.0070.08014.69
5.3.230.0100.07014.66
5.3.220.0030.05314.57
5.3.210.0170.06014.70
5.3.200.0100.07014.77
5.3.190.0070.08014.63
5.3.180.0100.04714.65
5.3.170.0100.06714.63
5.3.160.0030.04014.68
5.3.150.0130.07014.70
5.3.140.0030.07714.75
5.3.130.0130.06714.65
5.3.120.0070.03714.68
5.3.110.0130.07014.73
5.3.100.0130.06714.16
5.3.90.0100.06714.10
5.3.80.0070.05314.13
5.3.70.0170.06314.10
5.3.60.0030.07714.09
5.3.50.0070.05714.14
5.3.40.0130.07714.12
5.3.30.0030.07313.98
5.3.20.0070.07313.75
5.3.10.0030.07313.81
5.3.00.0000.08313.69

preferences:
47.95 ms | 400 KiB | 5 Q