()
| 1094 | class ClosureInvocationDemo |
| 1095 | { |
| 1096 | public function demo(): void |
| 1097 | { |
| 1098 | // Closure literal with native return type hint |
| 1099 | $makePen = function(): Pen { return new Pen(); }; |
| 1100 | $makePen()->write(); // resolves Pen from closure return type |
| 1101 | |
| 1102 | // Arrow function literal |
| 1103 | $makePencil = fn(): Pencil => new Pencil(); |
| 1104 | $makePencil()->sketch(); // arrow fn return type |
| 1105 | |
| 1106 | // Docblock callable annotation |
| 1107 | /** @var \Closure(): Pencil $supplier */ |
| 1108 | $supplier = getUnknownValue(); |
| 1109 | $supplier()->sharpen(); // @var Closure() annotation |
| 1110 | |
| 1111 | // Chaining after callable invocation |
| 1112 | $builder = function(): Pen { return new Pen(); }; |
| 1113 | $builder()->rename('Bold')->write(); // chain after $fn() |
| 1114 | |
| 1115 | // Variable assigned from callable invocation |
| 1116 | $fromClosure = $makePen(); |
| 1117 | $fromClosure->write(); // $result = $fn() resolves return type |
| 1118 | |
| 1119 | // Immediately invoked arrow function with return type |
| 1120 | $result = (fn(): Pen => new Pen())(); |
| 1121 | $result->write(); // resolves Pen from arrow fn return type |
| 1122 | |
| 1123 | // Immediately invoked closure with return type |
| 1124 | $obj = (function(): Pencil { return new Pencil(); })(); |
| 1125 | $obj->sketch(); // resolves Pencil from closure return type |
| 1126 | } |
| 1127 | } |
| 1128 | |
| 1129 |
nothing calls this directly
no test coverage detected