3v4l.org

run code in 300+ PHP versions simultaneously
<?php // test data class Foo { private $a; public function __construct($a) { $this->a = (int) $a; } public function hasGreaterAThan(self $another) { return $this->a > $another->a; } } $list = [ $foo1 = new Foo(2), $foo2 = new Foo(5), $foo3 = new Foo(1), ]; /** * Finds an item in a list via a comparison function * * @param array $aList the collection to examine * @param callable $comparison should compare two arguments and return a bool * @return mixed */ $find_by = function(array $aList, callable $comparison) { $callback = function($carry, $item) use ($comparison) { return ($carry == null || $comparison($item, $carry)) ? $item : $carry; }; return array_reduce($aList, $callback); }; $greaterA = function(Foo $foo1, Foo $foo2) { return $foo1->hasGreaterAThan($foo2); }; $lesserA = function(Foo $foo1, Foo $foo2) { return $foo2->hasGreaterAThan($foo1); }; assert($find_by($list, $greaterA) === $foo2); assert($find_by($list, $lesserA) === $foo3);
Output for git.master, git.master_jit, rfc.property-hooks

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:
33.26 ms | 405 KiB | 5 Q