Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?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