| 971 | class CollectionForeachDemo |
| 972 | { |
| 973 | public function demo(): void |
| 974 | { |
| 975 | $src = new ScaffoldingCollectionForeach(); |
| 976 | |
| 977 | // From method return type |
| 978 | foreach ($src->allPens() as $pen) { |
| 979 | $pen->write(); // via method return type → collection generics |
| 980 | } |
| 981 | |
| 982 | // From new instance |
| 983 | $items = new PenCollection(); |
| 984 | foreach ($items as $item) { |
| 985 | $item->color(); // resolves to Pen via @extends generics |
| 986 | } |
| 987 | |
| 988 | // From property type |
| 989 | foreach ($src->pens as $pen) { |
| 990 | $pen->color(); // via property type → collection generics |
| 991 | } |
| 992 | |
| 993 | // From variable |
| 994 | $collection = $src->allPens(); |
| 995 | foreach ($collection as $pen) { |
| 996 | $pen->write(); // via variable assignment scanning |
| 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | |