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 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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.
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
Fatal error: Uncaught TypeError: ArrayAnalyzer::__construct(): Argument #1 must be of type ArrayAnalyzerRule, array given, called in /in/YHs7h on line 97 and defined in /in/YHs7h:12 Stack trace: #0 /in/YHs7h(97): ArrayAnalyzer->__construct(Array) #1 {main} thrown in /in/YHs7h on line 12
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught TypeError: Argument 1 passed to ArrayAnalyzer::__construct() must implement interface ArrayAnalyzerRule, array given, called in /in/YHs7h on line 97 and defined in /in/YHs7h:12 Stack trace: #0 /in/YHs7h(97): ArrayAnalyzer->__construct(Array) #1 {main} thrown in /in/YHs7h on line 12
Process exited with code 255.
Output for 5.6.7 - 5.6.28
Catchable fatal error: Argument 1 passed to ArrayAnalyzer::__construct() must implement interface ArrayAnalyzerRule, array given, called in /in/YHs7h on line 98 and defined in /in/YHs7h on line 12
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/YHs7h on line 12
Process exited with code 255.

preferences:
152.7 ms | 401 KiB | 257 Q