1
我想使用此模式在我的代碼中啓用依賴注入。 我覺得它與動態語言的play-doh本質保持一致[1]。PHP嵌套靜態變量訪問依賴注入
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
也許這個工程:'$ this-> foo = {self :: $ FOO} :: getInstance();',但我不知道。不過,我不知道這應該是什麼樣的依賴注入?注入全局狀態?會有點多餘。 – hakre
所以在這種情況下,我正在使用傳統的類似rails的框架,它使用了大量普遍使用的靜態類。所以假設我必須調用Foo :: getInstance(),並且我不能徹底替換整個應用程序/測試套件中的Foo(並且我不能更改構造器參數)是否有另一種方法來注入一個實例富? –
$ this-> foo = {self :: $ FOO} :: getInstance()導致「語法錯誤,意外」{「」這是php 5.3.2 –