ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` <?php class Container { protected $bindings = []; public function bind($abstract, $concrete) { $this->bindings[$abstract] = $concrete; } public function make($abstract) { if (isset($this->bindings[$abstract])) { $concrete = $this->bindings[$abstract]; if ($concrete instanceof Closure) { return $concrete($this); } return $this->build($concrete); } throw new Exception("No binding found for {$abstract}"); } public function build($concrete) { // 在实际应用中,这里可能涉及更复杂的实例构建逻辑 return new $concrete; } } interface SomeInterface { public function someMethod(); } class SomeClass implements SomeInterface { public function someMethod() { return 'Hello, Laravel!'; } } // 创建容器实例 $container = new Container(); //用法1 // 绑定抽象类和具体实现类 $container->bind(SomeInterface::class, function ($container) { return new SomeClass(); }); //用法2 非闭包 //$container->bind(SomeInterface::class, new SomeClass()); // 解析实例 $instance = $container->make(SomeInterface::class); // 调用实例方法 $result = $instance->someMethod(); echo $result; ```