[英]PHP Nested Static Variable Access for Dependency Injection
我想使用此模式在代码中启用依赖项注入。 我觉得它与动态语言的“玩耍-玩法”性质保持一致[1]。
class A {
static $FOO = 'Foo';
function __construct() {
$this->foo = self::$FOO::getInstance();
}
}
A::$FOO = 'MockFoo';
$a = new A();
不幸的是,这不起作用,我得到:
Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in [test.php] on line 6
我可以创建一个临时变量来欺骗解析器,但是还有另一种方法吗?
function __construct() {
$FOO = self::$FOO;
$this->foo = $FOO::getInstance();
}
[1] http://weblog.jamisbuck.org/2008/11/9/legos-play-doh-and-programming
没有其他语法可以完成此操作。 您需要一个临时变量来欺骗解析器。
尝试
$class = self::$FOO;
$this->foo = $class::getInstance();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.