()
| 2880 | class CollectGenericDemo |
| 2881 | { |
| 2882 | public function demo(): void |
| 2883 | { |
| 2884 | /** @var Pen[] $pens */ |
| 2885 | $pens = []; |
| 2886 | |
| 2887 | // collect() uses function-level @template to carry element types |
| 2888 | // through to the returned FluentCollection. |
| 2889 | $collection = collect($pens); |
| 2890 | $collection->first()->write(); // TValue resolves to Pen |
| 2891 | |
| 2892 | // Inline chaining works too |
| 2893 | collect($pens)->first()->write(); // same resolution, no intermediate variable |
| 2894 | } |
| 2895 | } |
| 2896 | |
| 2897 |