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, $object, ReflectionMethod $method) use (&$data, $transform) { $normalized = preg_replace_callback('/([A-Z])/', $transform, $field); $data[$normalized] = $method->invoke($object); }); 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, $object, ReflectionMethod $method) use ($data, $transform) { $method->invoke($object, $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, $object, $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.0110.00418.68
8.3.50.0090.01321.96
8.3.40.0110.01119.08
8.3.30.0070.00719.21
8.3.20.0030.00618.98
8.3.10.0050.00521.22
8.3.00.0100.00022.03
8.2.180.0040.01816.63
8.2.170.0150.00022.96
8.2.160.0110.00320.60
8.2.150.0050.00324.18
8.2.140.0090.00024.66
8.2.130.0040.00426.16
8.2.120.0050.00321.17
8.2.110.0050.00519.52
8.2.100.0090.00318.09
8.2.90.0050.00319.30
8.2.80.0060.00317.97
8.2.70.0040.01117.61
8.2.60.0030.00518.16
8.2.50.0000.00918.07
8.2.40.0040.00418.41
8.2.30.0000.00719.50
8.2.20.0040.00417.89
8.2.10.0040.00417.91
8.2.00.0050.00318.00
8.1.280.0000.01525.92
8.1.270.0040.00424.66
8.1.260.0040.00426.35
8.1.250.0000.00828.09
8.1.240.0100.00022.17
8.1.230.0060.00621.19
8.1.220.0030.00518.76
8.1.210.0000.00818.77
8.1.200.0000.00917.60
8.1.190.0000.00817.66
8.1.180.0000.00818.10
8.1.170.0060.00318.75
8.1.160.0040.00419.02
8.1.150.0050.00318.76
8.1.140.0040.00417.58
8.1.130.0000.00717.81
8.1.120.0040.00417.71
8.1.110.0040.00417.66
8.1.100.0000.00717.59
8.1.90.0040.00417.60
8.1.80.0000.00717.66
8.1.70.0000.00717.67
8.1.60.0030.00617.76
8.1.50.0000.00817.70
8.1.40.0030.00517.65
8.1.30.0030.00617.85
8.1.20.0040.00417.80
8.1.10.0090.00017.89
8.1.00.0000.00817.75
8.0.300.0000.00819.88
8.0.290.0000.00817.18
8.0.280.0030.00518.66
8.0.270.0040.00417.30
8.0.260.0070.00017.05
8.0.250.0090.00017.29
8.0.240.0060.00317.28
8.0.230.0020.00517.36
8.0.220.0040.00417.08
8.0.210.0040.00417.09
8.0.200.0000.00717.13
8.0.190.0040.00417.26
8.0.180.0080.00017.19
8.0.170.0060.00317.24
8.0.160.0000.00917.15
8.0.150.0070.00017.10
8.0.140.0020.00517.22
8.0.130.0000.00613.67
8.0.120.0000.00717.04
8.0.110.0040.00417.04
8.0.100.0080.00017.06
8.0.90.0000.00817.25
8.0.80.0060.01117.19
8.0.70.0080.00017.16
8.0.60.0040.00417.04
8.0.50.0070.00017.14
8.0.30.0120.00817.25
8.0.20.0150.00417.41
8.0.10.0040.00417.34
8.0.00.0120.01017.21
7.4.330.0030.00316.86
7.4.320.0070.00016.80
7.4.300.0060.00016.80
7.4.290.0040.00416.93
7.4.280.0040.00416.88
7.4.270.0030.00316.81
7.4.260.0000.00613.57
7.4.250.0000.01016.89
7.4.240.0040.00416.73
7.4.230.0090.00016.75
7.4.220.0180.00416.70
7.4.210.0090.00716.91
7.4.200.0030.00416.87
7.4.190.0000.00816.77
7.4.160.0100.01316.89
7.4.150.0090.00917.40
7.4.140.0080.01117.86
7.4.130.0130.00816.89
7.4.120.0090.01016.85
7.4.110.0120.00616.71
7.4.100.0150.00616.83
7.4.90.0080.01216.55
7.4.80.0060.01419.39
7.4.70.0130.00616.92
7.4.60.0120.01316.84
7.4.50.0060.00316.81
7.4.40.0120.00622.77
7.4.30.0130.00716.90
7.4.00.0030.01215.34
7.3.330.0000.00513.58
7.3.320.0030.00313.57
7.3.310.0050.00216.66
7.3.300.0040.00416.51
7.3.290.0090.00616.61
7.3.280.0080.01016.63
7.3.270.0120.00617.40
7.3.260.0100.00818.24
7.3.250.0090.01016.62
7.3.240.0090.00916.62
7.3.230.0130.00616.92
7.3.210.0090.00916.84
7.3.200.0150.00419.39
7.3.190.0130.00616.87
7.3.180.0110.00716.60
7.3.170.0070.01116.91
7.3.160.0120.00616.90
7.3.120.0100.00714.99
7.2.330.0130.00716.93
7.2.320.0100.01417.05
7.2.310.0070.01116.96
7.2.300.0030.01317.05
7.2.290.0080.01016.96
7.2.00.0070.00719.58
7.1.100.0090.00618.59
7.1.70.0060.00317.07
7.1.60.0100.01619.40
7.1.50.0060.01817.29
7.1.00.0000.03722.30
7.0.200.4220.00315.34
7.0.140.0170.06321.95
7.0.100.0200.06020.05
7.0.90.0030.06319.96
7.0.80.0030.09020.01
7.0.70.0130.07720.09
7.0.60.0070.06020.17
7.0.50.0170.08320.45
7.0.40.0000.07720.05
7.0.30.0000.05320.00
7.0.20.0070.08020.15
7.0.10.0030.09019.99
7.0.00.0100.06020.07
5.6.280.0030.07720.96
5.6.250.0070.07720.83
5.6.240.0100.03720.68
5.6.230.0070.07320.55
5.6.220.0100.07720.73
5.6.210.0070.08320.64
5.6.200.0200.06721.14
5.6.190.0130.07021.06
5.6.180.0100.07721.10
5.6.170.0000.07021.09
5.6.160.0100.08021.23
5.6.150.0030.09021.06
5.6.140.0100.06721.09
5.6.130.0000.06321.07
5.6.120.0030.07021.07
5.6.110.0130.07021.18
5.6.100.0100.07021.10
5.6.90.0000.05021.04
5.6.80.0030.04320.46
5.6.70.0000.04720.57
5.6.60.0000.04320.48
5.6.50.0030.04320.50
5.6.40.0130.03720.38
5.6.30.0000.04720.45
5.6.20.0030.04020.37
5.6.10.0030.04320.30
5.6.00.0030.04320.51
5.5.380.0030.04720.42
5.5.370.0030.08720.41
5.5.360.0100.07320.45
5.5.350.0070.08720.46
5.5.340.0070.08020.98
5.5.330.0130.08320.86
5.5.320.0000.06320.94
5.5.310.0000.06020.79
5.5.300.0200.07020.84
5.5.290.0100.04320.83
5.5.280.0200.07020.90
5.5.270.0030.08020.90
5.5.260.0130.04320.95
5.5.250.0070.04320.77
5.5.240.0030.04320.24
5.5.230.0030.04020.19
5.5.220.0000.04320.25
5.5.210.0000.04320.24
5.5.200.0070.05320.29
5.5.190.0070.03720.14
5.5.180.0070.03720.30
5.5.160.0070.03320.31
5.5.150.0030.03720.27
5.5.140.0070.03320.20
5.5.130.0100.06320.20
5.5.120.0030.04020.34
5.5.110.0030.04020.31
5.5.100.0030.03320.23
5.5.90.0000.04720.13
5.5.80.0070.04320.13
5.5.70.0100.03320.17
5.5.60.0070.04020.09
5.5.50.0070.03720.09
5.5.40.0000.04320.11
5.5.30.0100.03320.08
5.5.20.0030.05720.11
5.5.10.0000.04720.12
5.5.00.0070.03720.09
5.4.450.0070.08719.38
5.4.440.0100.07319.35
5.4.430.0200.06719.52
5.4.420.0100.04719.27
5.4.410.0000.06319.32
5.4.400.0100.04019.10
5.4.390.0030.04719.13
5.4.380.0030.04319.03
5.4.370.0070.04019.14
5.4.360.0130.04019.09
5.4.350.0070.03319.23
5.4.340.0030.03318.88
5.4.320.0070.05019.14
5.4.310.0000.04019.05
5.4.300.0000.04019.03
5.4.290.0100.03319.20
5.4.280.0100.07019.24
5.4.270.0000.04318.95
5.4.260.0070.03318.95
5.4.250.0030.03319.04
5.4.240.0100.03318.84
5.4.230.0000.03719.22
5.4.220.0030.04319.13
5.4.210.0030.04018.87
5.4.200.0030.03319.13
5.4.190.0000.04019.14
5.4.180.0030.03719.18
5.4.170.0100.03019.22
5.4.160.0070.04019.13
5.4.150.0000.04719.11
5.4.140.0030.03716.39
5.4.130.0030.03716.38
5.4.120.0070.03716.51
5.4.110.0030.04316.45
5.4.100.0100.03016.56
5.4.90.0070.03016.32
5.4.80.0100.03016.55
5.4.70.0000.04016.54
5.4.60.0000.04016.39
5.4.50.0100.03016.41
5.4.40.0100.07316.49
5.4.30.0130.06016.38
5.4.20.0130.07016.42
5.4.10.0100.07016.45
5.4.00.0030.04315.84
5.3.290.0000.04014.73
5.3.280.0030.04014.66
5.3.270.0070.03014.60
5.3.260.0070.03714.73
5.3.250.0000.04014.75
5.3.240.0030.04314.75
5.3.230.0030.04314.74
5.3.220.0000.04014.71
5.3.210.0030.03714.68
5.3.200.0070.03314.70
5.3.190.0000.06314.53
5.3.180.0070.03014.57
5.3.170.0030.03714.67
5.3.160.0070.04014.65
5.3.150.0130.07014.59
5.3.140.0130.07314.76
5.3.130.0170.07014.69
5.3.120.0030.07314.58
5.3.110.0070.04314.70
5.3.100.0000.05314.17
5.3.90.0030.03714.02
5.3.80.0130.06714.01
5.3.70.0030.07714.08
5.3.60.0030.08014.06
5.3.50.0030.05014.00
5.3.40.0130.05314.07
5.3.30.0030.05014.05
5.3.20.0100.04313.90
5.3.10.0030.07013.75
5.3.00.0000.08313.69

preferences:
54.1 ms | 401 KiB | 5 Q