3v4l.org

run code in 300+ PHP versions simultaneously
<?php class MenuLinkTreeElement { public $link; public $subtree; public $depth; public $hasChildren; public $inActiveTrail; public $access; public $options; public function __construct($link, $has_children, $depth, $in_active_trail, array $subtree) { $this->link = $link; $this->hasChildren = $has_children; $this->depth = $depth; $this->subtree = $subtree; $this->inActiveTrail = $in_active_trail; $this->access = NULL; $this->options = array(); } public function count() { $sum = function ($carry, MenuLinkTreeElement $element) { return $carry + $element->count(); }; return 1 + array_reduce($this->subtree, $sum); } } // Arrays. $memory_before = memory_get_usage(); $array_start = microtime(TRUE); $storage = array(); for ($i = 0; $i < 1000; $i++) { $storage[] = array( 'link' => new StdClass(), 'has_children' => TRUE, 'depth' => 3, 'in_active_trail' => FALSE, 'below' => array(), 'access' => NULL, 'options' => array(), ); } $array_time = microtime(TRUE) - $array_start; $array_memory = memory_get_usage() - $memory_before; unset($storage); // Objects. $memory_before = memory_get_usage(); $object_start = microtime(TRUE); $storage = array(); for ($i = 0; $i < 1000; $i++) { $storage[] = new MenuLinkTreeElement(new StdClass(), TRUE, 3, FALSE, array()); } $object_time = microtime(TRUE) - $object_start; $object_memory = memory_get_usage() - $memory_before; unset($storage); echo 'arrays: ' . number_format($array_time, 3) . ' s, ' . ($array_memory/1024) . " KB\n"; echo 'objects: ' . number_format($object_time, 3) . ' s, ' . ($object_memory/1024) . " KB\n";

preferences:
33.09 ms | 402 KiB | 5 Q