首页 > 解决方案 > 有人可以解释以下代码如何调用 football() 方法,当我运行这个脚本时,它会得到“足球”的结果吗?

问题描述

<?php
    class container{
        protected $bindings = [];
        public function bind($name, callable $resolver){
            $this->bindings[$name] = $resolver;
        }

        public function make($name){
            //calls football() method , there is no football() method
            return $this->bindings[$name]();
        }
    }

    $container = new container;
    $container->bind('Game', function(){
        return 'football';
    });

    print_r($container->make('Game'));

代码调用football()方法,并 " football "在我运行它时返回结果。它如何运行football()不存在的方法?

标签: phpdependency-injectionclosuresinversion-of-control

解决方案


推荐阅读