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].
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
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.