Container.php
30 lines
| 1 | <?php declare(strict_types=1); |
| 2 | |
| 3 | namespace FapiMember\Container; |
| 4 | |
| 5 | use Exception; |
| 6 | |
| 7 | class Container |
| 8 | { |
| 9 | static array $instances = []; |
| 10 | |
| 11 | public static function set(string $name, mixed $instance): void |
| 12 | { |
| 13 | self::$instances[$name] = $instance; |
| 14 | } |
| 15 | |
| 16 | public static function get(string $name): mixed |
| 17 | { |
| 18 | if (!isset(self::$instances[$name])) { |
| 19 | // echo $name . '<br><br>'; |
| 20 | if (class_exists($name)) { |
| 21 | self::$instances[$name] = new $name(); |
| 22 | } else { |
| 23 | throw new Exception("Service not found: " . $name); |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return self::$instances[$name]; |
| 28 | } |
| 29 | } |
| 30 |