3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ArrayAnalyzer { /** * The rules to test. * * @var array */ protected $rules; public function __construct(ArrayAnalyzerRule ...$rules) { $this->rules = $rules; } /** * Analyzes an array with each of the rules, and returns the report. * * @param array $array * @return array */ public function analyze(array $array) { $report = []; foreach ($this->rules as $key => $rule) { if ($rule->fulfilledBy($array)) { $report[$key] = $rule->getValue($array); } } return $report; } } interface ArrayAnalyzerRule { /** * Checks whether the rule is fulfilled by the passed array. * * @param array $array * @return boolean */ public function fulfilledBy(array $array); /** * Gets the rule output value. * * @param array $array * @return array */ public function getValue(array $array); } class AllIntegersRule implements ArrayAnalyzerRule { /** * {@inheritDoc} */ public function fulfilledBy(array $array) { return array_reduce($array, function ($carry, $item) { return $carry && is_int($item); }, true); } /** * {@inheritDoc} */ public function getValue(array $array) { return true; } } class SameLengthRule implements ArrayAnalyzerRule { public function fulfilledBy(array $array) { return count(array_unique(array_map(function ($item) { return mb_strlen($item); }, $array))) === 1; } /** * {@inheritDoc} */ public function getValue(array $array) { return mb_strlen($array[0]); } } $arrayAnalyzer = new ArrayAnalyzer([ 'isInteger' => new AllIntegersRule, 'length' => new SameLengthRule, ]); var_dump($arrayAnalyzer->analyze([1, 2, 3])); var_dump($arrayAnalyzer->analyze([1, 2, 'D'])); var_dump($arrayAnalyzer->analyze([1, 2, '3D']));
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught TypeError: ArrayAnalyzer::__construct(): Argument #1 must be of type ArrayAnalyzerRule, array given, called in /in/YHs7h on line 95 and defined in /in/YHs7h:12 Stack trace: #0 /in/YHs7h(95): ArrayAnalyzer->__construct(Array) #1 {main} thrown in /in/YHs7h on line 12
Process exited with code 255.

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:
61.7 ms | 401 KiB | 8 Q