| 1506 | class ArrayFuncDemo |
| 1507 | { |
| 1508 | public function demo(): void |
| 1509 | { |
| 1510 | $src = new ScaffoldingArrayFunc(); |
| 1511 | |
| 1512 | $active = array_filter($src->members, fn(Pen $pen) => $pen->color() === 'blue'); |
| 1513 | $active[0]->write(); // Pen preserved through array_filter |
| 1514 | |
| 1515 | $vals = array_values($src->members); |
| 1516 | $vals[0]->write(); // Pen preserved through array_values |
| 1517 | |
| 1518 | $pens = $src->roster(); |
| 1519 | $last = array_pop($pens); |
| 1520 | $last->write(); // single Pen from array_pop |
| 1521 | |
| 1522 | $cur = current($src->members); |
| 1523 | $cur->write(); // Pen from current() |
| 1524 | |
| 1525 | end($src->members)->write(); // inline end() without variable |
| 1526 | |
| 1527 | foreach (array_filter($src->members, fn(Pen $pen) => true) as $pen) { |
| 1528 | $pen->color(); // Pen preserved in foreach |
| 1529 | } |
| 1530 | |
| 1531 | $mapped = array_map(fn($pen) => $pen, $src->members); |
| 1532 | $mapped[0]->write(); // Pen from array_map fallback |
| 1533 | |
| 1534 | // array_reduce: return type inferred from initial value (3rd arg) |
| 1535 | $merged = array_reduce($src->members, function(Pen $carry, Pen $item): Pen { |
| 1536 | return $carry; |
| 1537 | }, new Pen('merged')); |
| 1538 | $merged->write(); // Pen from initial value argument |
| 1539 | |
| 1540 | // array_sum / array_product: always int|float |
| 1541 | $total = array_sum([10, 20, 30]); |
| 1542 | $product = array_product([2, 3, 4]); |
| 1543 | } |
| 1544 | } |
| 1545 | |
| 1546 | |