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, 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) { $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.60.0140.00718.55
8.3.50.0100.01222.05
8.3.40.0120.00319.39
8.3.30.0040.01119.22
8.3.20.0060.00319.32
8.3.10.0040.00420.85
8.3.00.0120.00017.88
8.2.180.0040.01117.00
8.2.170.0060.01222.96
8.2.160.0070.01022.16
8.2.150.0080.00024.18
8.2.140.0000.00824.66
8.2.130.0040.00426.16
8.2.120.0090.00019.79
8.2.110.0110.00020.99
8.2.100.0030.01018.28
8.2.90.0000.00919.53
8.2.80.0000.00918.05
8.2.70.0060.00318.00
8.2.60.0040.00418.05
8.2.50.0000.00818.15
8.2.40.0000.00818.28
8.2.30.0000.00820.04
8.2.20.0030.00517.96
8.2.10.0060.00318.05
8.2.00.0000.00818.00
8.1.280.0090.00925.92
8.1.270.0030.00623.99
8.1.260.0030.00626.35
8.1.250.0060.00328.09
8.1.240.0060.00319.48
8.1.230.0060.00617.89
8.1.220.0050.00318.77
8.1.210.0000.00818.98
8.1.200.0060.00317.48
8.1.190.0000.00817.67
8.1.180.0040.00418.10
8.1.170.0030.00619.02
8.1.160.0040.00419.05
8.1.150.0000.00818.79
8.1.140.0020.00517.69
8.1.130.0000.00717.96
8.1.120.0000.00817.70
8.1.110.0000.00717.68
8.1.100.0030.00617.69
8.1.90.0080.00017.72
8.1.80.0040.00417.57
8.1.70.0040.00417.55
8.1.60.0060.00317.81
8.1.50.0030.00517.71
8.1.40.0040.00417.65
8.1.30.0030.00517.88
8.1.20.0050.00317.89
8.1.10.0000.00917.69
8.1.00.0000.00917.79
8.0.300.0070.00020.16
8.0.290.0040.00417.00
8.0.280.0000.00718.47
8.0.270.0030.00317.37
8.0.260.0000.00817.03
8.0.250.0060.00317.22
8.0.240.0110.00017.25
8.0.230.0040.00417.14
8.0.220.0070.00017.16
8.0.210.0040.00417.18
8.0.200.0040.00417.11
8.0.190.0040.00417.29
8.0.180.0040.00417.10
8.0.170.0060.00317.12
8.0.160.0000.00717.27
8.0.150.0000.00817.22
8.0.140.0060.00317.17
8.0.130.0000.00713.60
8.0.120.0000.00817.09
8.0.110.0000.01117.26
8.0.100.0040.00417.11
8.0.90.0000.00817.06
8.0.80.0110.01117.21
8.0.70.0000.00717.14
8.0.60.0040.00417.22
8.0.50.0040.00417.12
8.0.30.0090.01117.40
8.0.20.0120.01317.40
8.0.10.0040.00417.35
8.0.00.0170.00217.16
7.4.330.0060.00014.95
7.4.320.0060.00016.93
7.4.300.0000.00616.84
7.4.290.0030.00316.87
7.4.280.0000.00916.79
7.4.270.0030.00316.90
7.4.260.0080.00013.59
7.4.250.0030.00616.68
7.4.240.0070.00016.82
7.4.230.0000.00716.99
7.4.220.0200.00316.66
7.4.210.0090.00616.93
7.4.200.0000.00716.64
7.4.190.0040.00416.84
7.4.160.0000.01616.83
7.4.150.0160.00317.40
7.4.140.0110.00817.86
7.4.130.0070.01416.79
7.4.120.0100.00816.91
7.4.110.0130.01016.86
7.4.100.0090.00916.70
7.4.90.0090.00916.84
7.4.80.0060.01219.04
7.4.70.0000.01616.92
7.4.60.0150.00316.81
7.4.50.0060.00016.71
7.4.40.0040.01222.27
7.4.30.0070.01016.88
7.4.10.0140.00715.06
7.4.00.0150.00315.01
7.3.330.0030.00313.66
7.3.320.0030.00313.66
7.3.310.0000.00716.52
7.3.300.0030.00316.63
7.3.290.0000.01516.65
7.3.280.0110.00516.68
7.3.270.0070.01117.40
7.3.260.0070.01318.24
7.3.250.0130.00616.66
7.3.240.0070.01016.96
7.3.230.0100.00716.64
7.3.210.0060.01216.79
7.3.200.0070.01119.39
7.3.190.0080.01216.60
7.3.180.0050.01216.79
7.3.170.0090.00916.78
7.3.160.0130.00316.68
7.3.130.0160.00315.16
7.3.120.0060.00715.21
7.3.110.0070.01314.99
7.3.100.0030.01215.18
7.3.90.0060.00915.13
7.3.80.0000.01415.05
7.3.70.0070.00715.21
7.3.60.0000.00815.13
7.3.50.0120.00315.11
7.3.40.0030.01315.11
7.3.30.0080.00615.23
7.3.20.0090.00016.79
7.3.10.0090.00416.90
7.3.00.0060.00616.79
7.2.330.0120.01216.96
7.2.320.0160.00717.11
7.2.310.0150.00417.06
7.2.300.0120.00616.95
7.2.290.0050.01116.96
7.2.260.0040.01415.32
7.2.250.0090.00915.38
7.2.240.0040.01215.15
7.2.230.0060.00915.45
7.2.220.0090.00915.44
7.2.210.0000.01515.35
7.2.200.0090.00615.37
7.2.190.0080.00815.39
7.2.180.0100.00315.38
7.2.170.0000.01515.30
7.2.160.0000.01515.35
7.2.150.0060.00917.39
7.2.140.0040.01117.14
7.2.130.0030.01217.25
7.2.120.0000.01116.92
7.2.110.0090.00617.21
7.2.100.0070.01117.13
7.2.90.0030.01417.08
7.2.80.0000.01517.22
7.2.70.0090.00617.16
7.2.60.0000.01217.30
7.2.50.0000.01517.10
7.2.40.0070.00717.35
7.2.30.0000.01116.91
7.2.20.0040.00717.15
7.2.10.0060.00617.16
7.2.00.0080.00918.26
7.1.330.0040.01115.80
7.1.320.0060.00615.79
7.1.310.0070.00716.06
7.1.300.0070.00715.96
7.1.290.0060.00916.21
7.1.280.0030.01015.94
7.1.270.0100.00416.19
7.1.260.0110.00416.12
7.1.250.0080.00415.98
7.1.240.0030.01015.98
7.1.230.0040.01115.91
7.1.220.0070.00715.71
7.1.210.0060.00916.02
7.1.200.0060.00316.08
7.1.190.0070.00415.96
7.1.180.0000.01516.13
7.1.170.0100.00716.09
7.1.160.0030.00615.96
7.1.150.0000.01616.01
7.1.140.0080.00416.00
7.1.130.0070.00315.75
7.1.120.0030.01015.95
7.1.110.0130.00315.89
7.1.100.0030.01017.36
7.1.90.0030.00916.13
7.1.80.0000.01415.93
7.1.70.0030.01016.71
7.1.60.0060.01417.60
7.1.50.0050.01116.54
7.1.40.0060.00915.84
7.1.30.0000.01616.09
7.1.20.0110.00415.80
7.1.10.0090.00615.79
7.1.00.0040.04419.24
7.0.330.0040.01115.40
7.0.320.0000.01115.71
7.0.310.0030.00915.63
7.0.300.0090.00615.60
7.0.290.0030.00615.29
7.0.280.0000.01215.42
7.0.270.0060.00315.40
7.0.260.0120.00315.74
7.0.250.0030.00715.70
7.0.240.0060.00615.82
7.0.230.0070.01015.45
7.0.220.0060.00915.52
7.0.210.0030.01315.47
7.0.200.0020.00916.22
7.0.190.0060.00615.84
7.0.180.0030.00915.78
7.0.170.0070.01015.55
7.0.160.0000.01515.68
7.0.150.0030.01415.30
7.0.140.0080.03719.00
7.0.130.0070.00715.44
7.0.120.0080.00815.48
7.0.110.0070.01115.47
7.0.100.0080.02717.72
7.0.90.0050.02117.77
7.0.80.0070.02517.77
7.0.70.0100.02217.81
7.0.60.0050.02317.79
7.0.50.0080.02518.14
7.0.40.0020.02516.88
7.0.30.0020.04817.01
7.0.20.0090.03216.86
7.0.10.0120.03016.77
7.0.00.0030.03716.81
5.6.400.0120.00014.80
5.6.390.0070.00714.31
5.6.380.0040.01114.43
5.6.370.0030.01214.56
5.6.360.0050.00514.93
5.6.350.0040.00814.56
5.6.340.0060.00614.47
5.6.330.0130.00314.64
5.6.320.0060.00614.93
5.6.310.0000.01514.80
5.6.300.0060.01014.26
5.6.290.0070.00714.64
5.6.280.0030.04217.75
5.6.270.0030.01214.46
5.6.260.0060.01314.66
5.6.250.0050.02717.67
5.6.240.0050.02817.58
5.6.230.0050.02317.76
5.6.220.0050.02817.54
5.6.210.0050.05417.62
5.6.200.0050.02817.86
5.6.190.0060.02317.91
5.6.180.0080.02117.99
5.6.170.0100.03317.88
5.6.160.0080.03117.79
5.6.150.0060.02318.00
5.6.140.0100.04017.92
5.6.130.0050.03318.00
5.6.120.0030.04717.74
5.6.110.0060.02417.76
5.6.100.0060.05017.75
5.6.90.0050.03117.69
5.6.80.0050.02817.63
5.6.70.0080.02817.60
5.6.60.0130.04217.59
5.6.50.0120.02717.59
5.6.40.0080.03417.38
5.6.30.0050.02717.52
5.6.20.0100.03917.56
5.6.10.0040.04717.35
5.6.00.0070.04317.36
5.5.380.0050.03017.56
5.5.370.0030.02417.64
5.5.360.0050.02517.53
5.5.350.0080.02317.36
5.5.340.0050.03717.58
5.5.330.0080.02017.74
5.5.320.0090.03917.63
5.5.310.0070.02417.57
5.5.300.0110.04017.74
5.5.290.0070.03517.74
5.5.280.0080.04217.68
5.5.270.0050.04817.55
5.5.260.0090.04917.68
5.5.250.0160.03517.66
5.5.240.0070.03917.51
5.5.230.0010.02917.32
5.5.220.0070.04017.33
5.5.210.0030.04617.22
5.5.200.0040.02517.26
5.5.190.0020.02917.43
5.5.180.0080.04117.34
5.5.170.0070.00414.34
5.5.160.0070.04517.28
5.5.150.0060.02817.27
5.5.140.0020.03317.29
5.5.130.0060.02117.26
5.5.120.0050.04317.41
5.5.110.0020.03117.34
5.5.100.0020.04817.40
5.5.90.0030.04417.26
5.5.80.0070.02717.15
5.5.70.0060.02117.38
5.5.60.0060.02017.25
5.5.50.0060.02417.12
5.5.40.0080.02617.39
5.5.30.0050.02417.12
5.5.20.0030.02717.13
5.5.10.0110.03317.23
5.5.00.0000.04317.29
5.4.450.0060.03415.11
5.4.440.0050.04015.48
5.4.430.0130.03715.50
5.4.420.0120.03815.27
5.4.410.0090.04415.20
5.4.400.0050.03515.07
5.4.390.0060.02315.18
5.4.380.0070.02515.33
5.4.370.0050.02315.30
5.4.360.0070.03214.99
5.4.350.0030.03815.20
5.4.340.0020.02715.37
5.4.330.0000.00611.30
5.4.320.0140.03215.20
5.4.310.0050.04215.13
5.4.300.0020.03815.27
5.4.290.0050.02515.19
5.4.280.0120.03715.20
5.4.270.0110.03715.01
5.4.260.0060.02315.22
5.4.250.0050.03715.14
5.4.240.0080.03515.29
5.4.230.0040.04315.17
5.4.220.0060.02815.11
5.4.210.0050.03715.25
5.4.200.0090.03615.22
5.4.190.0030.03515.20
5.4.180.0080.02115.11
5.4.170.0070.02215.25
5.4.160.0050.04115.16
5.4.150.0040.03915.09
5.4.140.0050.03913.88
5.4.130.0050.02613.76
5.4.120.0080.02414.06
5.4.110.0000.03213.78
5.4.100.0140.02813.91
5.4.90.0070.03513.89
5.4.80.0050.03513.99
5.4.70.0130.03413.90
5.4.60.0030.03113.97
5.4.50.0030.04313.97
5.4.40.0030.03713.94
5.4.30.0100.02513.87
5.4.20.0040.04613.86
5.4.10.0100.03613.78
5.4.00.0050.03513.47
5.3.290.0070.03912.71
5.3.280.0070.03912.74
5.3.270.0080.01712.65
5.3.260.0100.02012.68
5.3.250.0020.04412.58
5.3.240.0030.04312.71
5.3.230.0070.03812.72
5.3.220.0050.04212.67
5.3.210.0070.04512.73
5.3.200.0050.02512.63
5.3.190.0070.02312.67
5.3.180.0100.03312.82
5.3.170.0060.03812.65
5.3.160.0080.03612.79
5.3.150.0050.02412.84
5.3.140.0080.02312.81
5.3.130.0030.04612.80
5.3.120.0020.04612.66
5.3.110.0070.04012.73
5.3.100.0050.03912.48
5.3.90.0000.04312.38
5.3.80.0080.02012.43
5.3.70.0140.03012.40
5.3.60.0080.03612.33
5.3.50.0070.03512.31
5.3.40.0050.03812.44
5.3.30.0070.03712.29
5.3.20.0000.04412.07
5.3.10.0050.02712.11
5.3.00.0050.02212.11

preferences:
46.09 ms | 401 KiB | 5 Q