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