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 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, $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) { $reflection = new ReflectionClass($object); $transform = function ($key) { return '_' . strtolower(array_shift($key)); }; $data = array(); 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(); } return $array; } } 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(); }); 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'); $hydrator = new ReflectionHydrator(); var_dump($hydrator->extract($book));

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.0070.00716.99
8.3.60.0070.01416.75
8.3.50.0130.00816.70
8.3.40.0090.00918.68
8.3.30.0060.00918.54
8.3.20.0060.00318.63
8.3.10.0060.00320.36
8.3.00.0030.00619.13
8.2.180.0100.01016.75
8.2.170.0120.00922.96
8.2.160.0130.00720.98
8.2.150.0030.00524.18
8.2.140.0030.00624.66
8.2.130.0050.00326.16
8.2.120.0080.00019.71
8.2.110.0070.00321.01
8.2.100.0120.00017.78
8.2.90.0030.00517.91
8.2.80.0000.00817.97
8.2.70.0030.00617.93
8.2.60.0040.00418.34
8.2.50.0000.00818.10
8.2.40.0000.00718.34
8.2.30.0000.00918.15
8.2.20.0000.00717.61
8.2.10.0040.00417.66
8.2.00.0100.00017.71
8.1.280.0040.01125.92
8.1.270.0040.00424.66
8.1.260.0090.00026.35
8.1.250.0080.00028.09
8.1.240.0040.00422.46
8.1.230.0060.00821.01
8.1.220.0080.00017.78
8.1.210.0060.00318.77
8.1.200.0030.00617.23
8.1.190.0080.00017.35
8.1.180.0080.00018.10
8.1.170.0000.00818.62
8.1.160.0070.00018.92
8.1.150.0030.00518.66
8.1.140.0080.00017.36
8.1.130.0030.00317.85
8.1.120.0000.00817.43
8.1.110.0040.00417.52
8.1.100.0000.00817.38
8.1.90.0000.00717.39
8.1.80.0040.00417.48
8.1.70.0000.00717.42
8.1.60.0040.00417.50
8.1.50.0040.00417.54
8.1.40.0040.00417.54
8.1.30.0040.00417.50
8.1.20.0030.00617.74
8.1.10.0000.00817.32
8.1.00.0000.00817.43
8.0.300.0050.00219.72
8.0.290.0040.00416.88
8.0.280.0000.00718.34
8.0.270.0050.00317.18
8.0.260.0070.00016.76
8.0.250.0000.00816.98
8.0.240.0040.00416.93
8.0.230.0070.00016.93
8.0.220.0030.00516.92
8.0.210.0000.00716.98
8.0.200.0060.00016.95
8.0.190.0050.00317.04
8.0.180.0080.00016.99
8.0.170.0060.00316.95
8.0.160.0040.00416.88
8.0.150.0030.00616.93
8.0.140.0030.00516.95
8.0.130.0030.00313.40
8.0.120.0040.00416.88
8.0.110.0000.00717.04
8.0.100.0050.00216.93
8.0.90.0000.00717.08
8.0.80.0160.00316.93
8.0.70.0040.00416.95
8.0.60.0030.00516.76
8.0.50.0070.00016.84
8.0.30.0120.01117.26
8.0.20.0100.01117.14
8.0.10.0000.00817.09
8.0.00.0150.00416.82
7.4.330.0030.00313.05
7.4.320.0070.00016.54
7.4.300.0030.00316.39
7.4.290.0060.00316.58
7.4.280.0040.00416.63
7.4.270.0060.00016.62
7.4.260.0030.00313.21
7.4.250.0040.00416.41
7.4.240.0040.00316.52
7.4.230.0030.00316.52
7.4.220.0120.00616.53
7.4.210.0040.01016.51
7.4.200.0030.00416.65
7.4.190.0070.00016.71
7.4.160.0080.00816.45
7.4.150.0120.00616.50
7.4.140.0050.01216.51
7.4.130.0050.01216.53
7.4.120.0120.00716.49
7.4.110.0090.00916.55
7.4.100.0100.01316.52
7.4.90.0060.01116.54
7.4.80.0030.01416.53
7.4.70.0100.00716.54
7.4.60.0150.01016.68
7.4.50.0030.00616.62
7.4.40.0040.01116.59
7.4.30.0110.00716.49
7.4.10.0120.00815.08
7.4.00.0100.00615.08
7.3.330.0000.00713.19
7.3.320.0030.00313.19
7.3.310.0070.00016.37
7.3.300.0070.00016.28
7.3.290.0090.00616.33
7.3.280.0080.01316.33
7.3.270.0090.00916.18
7.3.260.0140.00916.59
7.3.250.0160.00416.34
7.3.240.0100.01016.53
7.3.230.0130.00316.52
7.3.210.0090.00916.32
7.3.200.0150.00319.39
7.3.190.0120.00416.48
7.3.180.0060.01016.46
7.3.170.0090.01316.54
7.3.160.0060.01016.61
7.3.130.0040.01215.06
7.3.120.0060.01114.95
7.3.110.0040.01414.74
7.3.100.0100.00314.95
7.3.90.0030.00915.02
7.3.80.0030.01015.09
7.3.70.0060.00614.81
7.3.60.0080.00814.56
7.3.50.0030.00914.67
7.3.40.0080.00814.76
7.3.30.0070.00314.64
7.3.20.0000.01116.32
7.3.10.0100.00716.69
7.3.00.0000.01416.51
7.2.330.0090.01516.54
7.2.320.0140.00616.55
7.2.310.0100.01016.72
7.2.300.0070.01016.47
7.2.290.0100.00716.35
7.2.260.0060.00614.96
7.2.250.0060.01215.13
7.2.240.0070.00715.04
7.2.230.0030.01015.23
7.2.220.0060.00315.04
7.2.210.0030.01015.07
7.2.200.0070.00715.20
7.2.190.0000.00914.92
7.2.180.0060.00614.97
7.2.170.0000.01615.12
7.2.160.0120.00314.90
7.2.150.0090.00616.73
7.2.140.0070.01016.49
7.2.130.0090.00616.52
7.2.120.0030.01216.74
7.2.110.0080.00616.74
7.2.100.0040.00716.73
7.2.90.0100.01016.72
7.2.80.0060.00916.78
7.2.70.0130.00316.74
7.2.60.0030.00916.73
7.2.50.0110.00616.89
7.2.40.0090.00616.86
7.2.30.0000.01016.90
7.2.20.0070.00716.93
7.2.10.0070.00716.94
7.2.00.0420.01317.18
7.1.330.0000.01215.82
7.1.320.0080.00315.75
7.1.310.0090.00615.26
7.1.300.0060.00315.85
7.1.290.0050.00515.63
7.1.280.0080.00415.66
7.1.270.0060.00915.72
7.1.260.0000.01515.83
7.1.250.0090.00015.86
7.1.240.0060.01015.46
7.1.230.0060.00615.71
7.1.220.0040.01115.61
7.1.210.0070.00715.69
7.1.200.0070.00615.66
7.1.190.0100.00315.56
7.1.180.0100.00015.61
7.1.170.0030.01015.73
7.1.160.0150.00315.80
7.1.150.0000.01015.82
7.1.140.0070.00715.50
7.1.130.0030.01015.73
7.1.120.0000.01315.65
7.1.110.0030.01015.76
7.1.100.0050.00616.58
7.1.90.0060.00615.75
7.1.80.0040.00815.34
7.1.70.0020.00816.20
7.1.60.0100.00817.61
7.1.50.0100.01116.14
7.1.40.0040.00715.43
7.1.30.0070.00715.71
7.1.20.0060.00615.47
7.1.10.0080.00415.41
7.1.00.0110.03619.14
7.0.330.0030.01315.21
7.0.320.0000.01415.25
7.0.310.0060.00615.22
7.0.300.0070.01015.33
7.0.290.0040.00815.48
7.0.280.0100.00315.25
7.0.270.0000.01315.02
7.0.260.0030.01015.41
7.0.250.0000.01415.29
7.0.240.0100.00315.42
7.0.230.0040.00414.91
7.0.220.0090.00015.32
7.0.210.0090.00315.35
7.0.200.0050.00615.98
7.0.190.0070.00715.31
7.0.180.0030.01015.26
7.0.170.0000.01115.30
7.0.160.0080.00015.46
7.0.150.0090.00015.26
7.0.140.0080.03818.61
7.0.130.0030.00915.47
7.0.120.0030.04018.68
7.0.110.0070.04817.71
7.0.100.0050.02317.55
7.0.90.0100.03717.72
7.0.80.0120.04017.56
7.0.70.0070.04017.45
7.0.60.0120.03517.71
7.0.50.0090.03017.73
7.0.40.0040.02616.57
7.0.30.0050.04516.42
7.0.20.0030.02516.65
7.0.10.0080.02016.61
7.0.00.0070.03016.55
5.6.400.0070.00714.25
5.6.390.0130.00314.04
5.6.380.0040.01114.48
5.6.370.0040.00814.14
5.6.360.0100.01014.27
5.6.350.0060.00914.40
5.6.340.0070.00714.08
5.6.330.0030.00714.27
5.6.320.0000.01414.48
5.6.310.0060.00314.34
5.6.300.0090.00914.39
5.6.290.0100.00614.46
5.6.280.0070.03717.56
5.6.270.0070.00314.48
5.6.260.0040.04017.56
5.6.250.0020.02717.50
5.6.240.0070.02217.44
5.6.230.0050.04517.51
5.6.220.0110.03517.37
5.6.210.0040.02917.44
5.6.200.0020.02517.33
5.6.190.0000.02817.43
5.6.180.0040.02917.58
5.6.170.0090.02417.49
5.6.160.0040.04117.56
5.6.150.0100.02717.42
5.6.140.0070.02517.48
5.6.130.0070.02517.50
5.6.120.0030.02417.45
5.6.110.0070.05017.50
5.6.100.0100.03217.54
5.6.90.0050.02417.59
5.6.80.0140.03217.13
5.6.70.0080.02717.07
5.6.60.0040.02517.28
5.6.50.0100.02217.19
5.6.40.0070.02316.90
5.6.30.0070.03317.10
5.6.20.0050.02517.14
5.6.10.0050.02717.06
5.6.00.0080.02217.09
5.5.380.0050.04115.74
5.5.370.0070.03615.87
5.5.360.0050.03315.83
5.5.350.0050.02915.81
5.5.340.0020.02515.96
5.5.330.0020.03016.00
5.5.320.0070.04016.24
5.5.310.0080.04216.04
5.5.300.0050.03716.10
5.5.290.0010.03316.12
5.5.280.0030.02516.14
5.5.270.0030.03116.05
5.5.260.0070.03416.15
5.5.250.0070.02316.05
5.5.240.0090.04015.72
5.5.230.0070.02315.79
5.5.220.0030.02315.84
5.5.210.0090.01915.69
5.5.200.0050.03815.69
5.5.190.0020.03615.81
5.5.180.0090.03715.69
5.5.170.0030.01014.13
5.5.160.0090.03715.70
5.5.150.0050.03315.82
5.5.140.0070.04215.49
5.5.130.0070.02415.79
5.5.120.0090.03415.74
5.5.110.0130.03515.53
5.5.100.0020.04315.68
5.5.90.0050.04315.74
5.5.80.0050.02915.78
5.5.70.0030.03215.96
5.5.60.0090.03415.61
5.5.50.0050.03915.78
5.5.40.0100.03715.79
5.5.30.0080.02315.55
5.5.20.0080.04115.56
5.5.10.0060.04015.52
5.5.00.0030.04315.47
5.4.450.0050.02216.09
5.4.440.0020.02516.14
5.4.430.0050.02916.09
5.4.420.0050.04316.18
5.4.410.0040.02615.96
5.4.400.0010.02415.91
5.4.390.0080.02315.83
5.4.380.0040.02515.87
5.4.370.0050.02215.93
5.4.360.0090.01815.93
5.4.350.0040.04015.86
5.4.340.0070.02815.98
5.4.330.0000.01412.71
5.4.320.0020.03015.88
5.4.310.0050.03716.03
5.4.300.0040.02215.87
5.4.290.0030.02415.85
5.4.280.0080.04515.94
5.4.270.0050.03515.97
5.4.260.0050.02416.01
5.4.250.0070.03715.85
5.4.240.0070.02716.02
5.4.230.0050.04015.98
5.4.220.0050.04115.95
5.4.210.0050.02815.90
5.4.200.0080.03715.83
5.4.190.0060.03315.87
5.4.180.0030.03115.93
5.4.170.0050.03015.97
5.4.160.0020.02515.82
5.4.150.0060.02315.96
5.4.140.0020.04214.68
5.4.130.0070.03014.66
5.4.120.0060.02014.68
5.4.110.0050.03614.68
5.4.100.0030.03814.54
5.4.90.0050.02914.60
5.4.80.0050.02014.51
5.4.70.0120.03214.56
5.4.60.0090.03414.65
5.4.50.0090.03014.67
5.4.40.0080.02014.58
5.4.30.0090.01814.64
5.4.20.0060.02114.59
5.4.10.0060.03114.57
5.4.00.0050.02514.38
5.3.290.0030.03313.79
5.3.280.0010.03013.70
5.3.270.0080.03513.70
5.3.260.0080.01813.71
5.3.250.0070.02613.70
5.3.240.0040.03413.63
5.3.230.0070.02013.70
5.3.220.0020.03313.72
5.3.210.0080.02513.74
5.3.200.0040.02113.67
5.3.190.0030.04013.75
5.3.180.0040.02313.67
5.3.170.0050.02313.66
5.3.160.0070.04113.68
5.3.150.0050.02113.79
5.3.140.0080.03313.74
5.3.130.0050.04113.66
5.3.120.0020.03513.71
5.3.110.0080.03413.68
5.3.100.0110.02213.52
5.3.90.0090.01913.45
5.3.80.0080.01813.41
5.3.70.0070.03213.41
5.3.60.0030.02513.40
5.3.50.0060.03813.40
5.3.40.0060.01813.41
5.3.30.0050.02913.38
5.3.20.0070.02013.27
5.3.10.0030.02513.23
5.3.00.0060.03113.23

preferences:
75.34 ms | 401 KiB | 5 Q