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)
5.4.150.0110.04712.44
5.4.140.0200.05412.13
5.4.130.0130.04012.11
5.4.120.0130.04112.08
5.4.110.0120.04112.07
5.4.100.0160.05912.07
5.4.90.0120.04612.07
5.4.80.0120.04312.07
5.4.70.0170.06612.07
5.4.60.0090.04412.07
5.4.50.0160.03912.07
5.4.40.0170.05912.05
5.4.30.0120.05712.05
5.4.20.0180.06012.05
5.4.10.0130.04412.05
5.4.00.0100.04511.54
5.3.250.0110.04812.76
5.3.240.0140.04512.75
5.3.230.0140.04312.75
5.3.220.0120.04612.72
5.3.210.0180.06212.72
5.3.200.0150.04212.71
5.3.190.0140.04412.71
5.3.180.0140.04312.71
5.3.170.0110.04512.71
5.3.160.0130.04312.71
5.3.150.0150.04212.71
5.3.140.0150.04212.70
5.3.130.0120.04512.70
5.3.120.0180.03912.70
5.3.110.0170.06012.70
5.3.100.0160.04212.18
5.3.90.0120.04412.17
5.3.80.0120.05512.15
5.3.70.0130.04412.15
5.3.60.0180.03912.14
5.3.50.0130.04312.09
5.3.40.0120.04212.09
5.3.30.0160.03912.05
5.3.20.0130.04211.83
5.3.10.0170.03611.79
5.3.00.0120.04211.78

preferences:
142.09 ms | 1394 KiB | 7 Q