PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Numbers { private array $collection = []; public function __construct(...$nums) { $this->collection = array_filter($nums, fn($num) => is_numeric($num)); } public function toArray() { return $this->collection; } } class SampleClass2 { public static function add(Numbers $numbers) { $arr = $numbers->toArray(); return match(count($arr)) { 2 => array_sum($arr), 3 => array_sum($arr) > 10 ? 10 : array_sum($arr) }; } } $twentyfour = new Numbers(12, 12); $ten = new Numbers(12, 2, 6); echo SampleClass2::add($twentyfour) . PHP_EOL; // Outputs 24 echo SampleClass2::add($ten) . PHP_EOL; // outputs 10
Show:  
Copy Clear