3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface Arrayable { public function toArray(); } class Item implements Arrayable { protected $attributes = array(); protected $someParameter = true; public function __construct($attributes) { $this->attributes = $attributes; } public function toArray() { return $this->attributes; } } class ItemCollection implements Arrayable { protected $items = array(); public function addItem(Item $item) { $this->items[] = $item; } public function toArray() { $result = array(); foreach ($this->items as $item) { $result[] = $item->toArray(); } return $result; } } $array = array('foo', 'bar', 'baz'); $item = new Item($array); var_dump((array) $item); var_dump($item->toArray()); $collection = new ItemCollection(); $collection->addItem($item); $collection->addItem($item); var_dump((array) $collection); var_dump($collection->toArray());

preferences:
36.26 ms | 402 KiB | 5 Q