ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
依赖注入 容器的 小刀牛试 ``` <?php /** * Created by PhpStorm. * User: linjinkun * Date: 2019/10/14 * Time: 22:37 */ //轮胎类 ===》汽车类 class LunTai { function roll(){ echo '轮胎在滚动<br>'; } } class BMW { protected $luntai; function __construct($lun_tai){ $this->luntai = $lun_tai; } function run(){ $this->luntai->roll(); echo '开着宝马吃烤串<br>'; } } // $lun_tai = new LunTai(); // $bmw = new BMW($lun_tai); // $bmw->run(); class Container { //存放绑定的类 static $register = []; static function bind($name,Closure $col){ self::$register[$name] = $col; } //创建函数对象 static function make($name){ $col = self::$register[$name]; return $col(); } } Container::bind('luntai',function(){return new LunTai();}); Container::bind('BMW',function(){ return new BMW(Container::make('luntai'));}); $bmw = Container::make('BMW'); $bmw->run(); ```