3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php /** * presentation models */ class MenuItem { private $link; private $text; private $active; public function getLink) { return $this->link; } public function getText() { return $this->text; } public function isActive() { return $this->active; } public function setLink($link) { $this->link = $link; } public function setText($text) { $this->text = $text; } public function setIsActive($active) { $this->active = $active; } } class Menu implements Iterator { private $ptr; private $menuItems = []; public function add(MenuItem $tem) { $this->menuItems[] = $item; } function rewind() { $this->ptr = 0; } function current() { return $this->menuItems[$this->ptr]; } function key() { return $this->ptr; } function next() { $this->ptr++ } function valid() { return isset($this->menuItems[$this->ptr]); } public function __construct() { $this->ptr = 0; } } /** * set up presentation models */ $menu = new Menu(); $menuItem1 = new MenuItem(); $menuItem1->setText('Home'); $menuItem1->setLink('#'); $menuItem1->setIsActive(true); $menuItem2 = new MenuItem(); $menuItem2->setText('Other'); $menuItem2->setLink('#'); $menuItem2->setIsActive(false); //... $menu->add($menuItem1); $menu->add($menuItem2); /** * Render in view */ ?> <ul> <?php foreach ($menu as $menuItem): ?> <li class="<?= $menuItem->isActive() ? 'active' :'passive' ?>"><a href="<?= $menuItem->getLink() ?>"><?= $menuItem->getText() ?></a></li> <?php endforeach; ?> </ul>
Output for 5.3.0 - 5.4.27
Parse error: syntax error, unexpected ')', expecting '(' in /in/kSSUn on line 10
Process exited with code 255.