【发布时间】:2026-01-25 15:15:02
【问题描述】:
我很难理解为什么下面的代码不能按预期工作。我通常使用bucket 依赖注入器,但为了简化,我在下面使用了twittee,结果完全相同。
那么为什么我修改的Config-object(和创建的设置元素)不能用于MockObject-class?据我所知,它被正确传递了。
代码:
$c = new Container();
$c->config = function ($c) { return new Config(); };
$config = $c->config;
$config->setting = 'a value';
$c->MockObject = function ($c) { return new MockObject($c->config); };
$c->MockObject; // Error thrown: `Notice: Undefined index: setting`
类:
class Container {
protected $s=array();
function __set($k, $c) { $this->s[$k]=$c; }
function __get($k) { return $this->s[$k]($this); }
}
class Config {
public $values = array();
function __set($key, $value){ $this->values[$key] = $value; }
function __get($key) { return $this->values[$key]; }
}
class MockObject {
function __construct(Config $config) { echo $config->setting; }
}
依赖注入容器由 Fabien Potencier 提供; https://github.com/fabpot/twittee
【问题讨论】:
-
“我在下面使用了 twittee,结果完全相同。”你得到了什么结果?
-
注意:更强大的 twitee 版本是 Pimple:pimple-project.org 也来自 Fabien Potencier。
-
是的 - 知道这一点。只是用 twitee 来展示我在这里做什么
标签: php oop dependency-injection