3v4l.org

run code in 300+ PHP versions simultaneously
<?php class A { public $a; public $b; private $c; protected static $columnRegistry = array(); protected $columns = array(); public function __construct($a, $b, $c) { $this->a = $a; $this->b = $b; $this->c = $c; $this->setupColumns(); } public function toArray() { $ret = []; foreach ($this->columns as $column) { $ret[$column] = $this->$column; } return $ret; } protected function setupColumns() { $class = get_class($this); // Define columns for current row if (!array_key_exists($class, self::$columnRegistry)) { foreach ((new ReflectionClass($this))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { if (!$property->isStatic()) { $this->columns[] = $property->getName(); } } self::$columnRegistry[$class] = $this->columns; } else { $this->columns = self::$columnRegistry[$class]; } } } $expected = [ 'a' => 1, 'b' => 2, ]; $numReps = 100000; $start = microtime(true); for($i = 0; $i < $numReps; $i++) { $o = new A(1, 2, 3); $res = $o->toArray(); if ($res !== $expected) { throw new Exception("Not same"); } } $end = microtime(true); $mem = memory_get_peak_usage(true); echo number_format($end-$start, 8) . "\t$mem\n";

preferences:
34.75 ms | 402 KiB | 5 Q