| 1805 | class MatchClassStringDemo |
| 1806 | { |
| 1807 | public function demo(): void |
| 1808 | { |
| 1809 | $container = new Container(); |
| 1810 | |
| 1811 | // Match expression → class-string → conditional return |
| 1812 | $requestType = match (rand(0, 1)) { |
| 1813 | 0 => ElasticProductReviewIndexService::class, |
| 1814 | 1 => ElasticBrandIndexService::class, |
| 1815 | }; |
| 1816 | $requestBody = $container->make($requestType); |
| 1817 | $requestBody->index(); // on both classes |
| 1818 | $requestBody->reindex(); // ElasticProductReviewIndexService only |
| 1819 | |
| 1820 | // Standalone function with @template |
| 1821 | $resolved = resolve($requestType); |
| 1822 | $resolved->index(); // on both classes |
| 1823 | |
| 1824 | // Inline chain |
| 1825 | $container->make($requestType)->index(); |
| 1826 | |
| 1827 | // Simple class-string variable |
| 1828 | $cls = Pen::class; |
| 1829 | $pen = $container->make($cls); |
| 1830 | $pen->write(); // resolves through simple $cls variable |
| 1831 | |
| 1832 | // Ternary class-string |
| 1833 | $ternary = rand(0, 1) ? Pen::class : Pencil::class; |
| 1834 | $obj = $container->make($ternary); |
| 1835 | $obj->label(); // shared by both types |
| 1836 | } |
| 1837 | } |
| 1838 | |
| 1839 | |