3v4l.org

run code in 300+ PHP versions simultaneously
<?php $category = function(string $name, $parent = null) { return new class($name, $parent) { public $name; public $parent; public function __construct(string $name, $parent = null) { $this->name = $name; $this->parent = $parent; } public function breadcrumbs(array $trail = [], ?callable $decorator = null) { $decorator = $decorator ?? function(string $item) { return $item; }; $trail[] = $decorator($this->name); return $this->parent ? $this->parent->breadcrumbs($trail, $decorator) : $trail; } }; }; $categorize = function(...$categories) use($category) { return array_reduce(array_reverse($categories), function($set, $item) use($category) { if (!$set) { return $category($item); } return $category($item, $set); }); }; $product = function(string $name, string $sku, ...$categories) use($categorize) { return new class($name, $sku, $categorize(...$categories)) { public $name; public $categories; public $sku; public function __construct(string $name, string $sku, $categories) { $this->name = $name; $this->sku = $sku; $this->categories = $categories; } public function trail(string $separator = ' / ', ?callable $decorator = null): string { return implode($separator, $this->categories->breadcrumbs([], $decorator)); } public function linkSimilar(string $separator = ' / '): string { return $this->trail($separator, function(string $name) { return sprintf('<a href="/category/%s?similar-to=product/%s">%s</a>', $name, $this->sku, $name); }); } }; }; $book = $product('Harry Potter', 'U425261', 'books', 'hardback', 'young-adult'); $shoes = $product('Air Jordans', 'U425262', 'shoes', 'mens', 'sports', 'basketball'); $tapeMeasure = $product('Stanley 25', 'U425263', 'hardware', 'tools', 'measurement'); var_dump( $book->trail(), $shoes->trail(' | '), $tapeMeasure->linkSimilar(PHP_EOL.' > ') );
Output for git.master, git.master_jit, rfc.property-hooks
string(30) "books / hardback / young-adult" string(34) "shoes | mens | sports | basketball" string(212) "<a href="/category/hardware?similar-to=product/U425263">hardware</a> > <a href="/category/tools?similar-to=product/U425263">tools</a> > <a href="/category/measurement?similar-to=product/U425263">measurement</a>"

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
90.2 ms | 406 KiB | 5 Q