()
| 751 | class ForeachByReferenceDemo |
| 752 | { |
| 753 | public function demo(): void |
| 754 | { |
| 755 | /** @var list<Pen> $pens */ |
| 756 | $pens = getUnknownValue(); |
| 757 | |
| 758 | // By-reference foreach: $pen resolves to element type (Pen) |
| 759 | // and is not flagged as undefined or unused. |
| 760 | foreach ($pens as &$pen) { |
| 761 | $pen->write(); // Pen from list<Pen> |
| 762 | $pen = new Pen(); // reassignment through reference |
| 763 | } |
| 764 | unset($pen); |
| 765 | |
| 766 | // Key-value with by-reference value |
| 767 | /** @var array<string, Pen> $named */ |
| 768 | $named = getUnknownValue(); |
| 769 | foreach ($named as $key => &$tool) { |
| 770 | $tool->color(); // Pen from array<string, Pen> |
| 771 | } |
| 772 | unset($tool); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 |
nothing calls this directly
no test coverage detected