1
我有點困惑,在構造函數或工廠方法中設置對象屬性的正確位置是什麼,或者它並不重要?工廠或構造函數 - 在哪裏設置對象屬性?
在構造函數中:
class Foo {
public $bar;
function __constructor($b) {
$this->bar = $b;
}
static function factory($b) {
return new self($b);
}
}
工廠:
class Foo {
public $bar;
static function factory($b) {
$obj = new self();
$obj->bar = $b;
return $obj;
}
}
在你的工廠方法和構造函數中做同樣的事情。對於你的代碼是沒有關係的。 – pltvs
就工廠而言,它沒有區別,因爲它是封裝:) – hakre
@JaredFarrish更正,謝謝! –