匿名函数调用:“致命错误:函数名称必须是字符串”

时间:2014-05-08 14:59:35

标签: php dependency-injection anonymous-function

这是依赖注入的我的类“容器”。

当我调用$ this-> get($ service)时,我有“致命错误:函数名称必须是字符串”。

我之前设置了服务($ this-> set($ service)),但始终存在同样的问题。

一个想法?

<?php

namespace Core;

class Container {

    private $_container             = array();
    private $_containerSingleton    = array();

    public function __construct(ServicesInterface $services){
        $this->_initServicesFromConfiguration($services->get());
    }

    private function _initServicesFromConfiguration($services){
        foreach ($services as $service){
            $this->set($service);
        }
    }

    public function set($service){
        $this->_container[$service] = function() use ($service) {
                                    return new $service();
                                  };
    }

    public function get($service){
        return $this->_container[$service]();
    }

    public function share($service){
        if(isset($this->_containerSingleton[$service])){
            return $this->_containerSingleton[$service];
        }
        $this->_containerSingleton[$service] = $this->get($service);
        return $this->_containerSingleton[$service];
    }

}

?>

0 个答案:

没有答案