1
我Chart
類實際上允許我創建簡單的屬性(string
型,boolean
等等),以及嵌套object
性能調用魔術__call
方法是這樣的:以最小的努力創建任意對象層次結構?
$chart = new Chart();
$chart->simple = 'Hello';
$chart->newComplex();
var_dump($chart);
輸出:
object(Chart)[1]
public 'simple' => string 'Hello' (length=5)
public 'complex' =>
object(stdClass)[2]
我想添加能力來創建嵌套object
屬性作爲其他屬性的子女(不是圖表本身的子女)這樣的:
$chart->newComplex2($chart->newComplex1());
問題是:如何使用$args
參數和修改__call()
做到這一點?
class Chart
{
public function __call($name, $args)
{
$type = substr($name, 0, 3);
$field = lcfirst(substr($name, strlen($type)));
switch($type)
{
case 'get': return isset($this->$field) ? $this->$field : null;
case 'new': return $this->$field = new stdClass();
}
}
}
作爲一個建議,首先提供您正在尋找的功能並接受'$ args'數組或從'func_get_args'創建它的函數的非魔術函數代碼。您稍後可以繼續添加魔術界面。在你的代碼示例中,'$ args [0]'是你想知道的第一個參數。 – hakre 2012-01-08 11:04:09
您不應該混合協作者和創作者圖。你的對象應該有單一的責任。允許他們創建對象是一項責任。將其分解爲工廠和生成器模式。 – Gordon 2012-01-08 11:28:17
@戈登感謝您的提示。該圖表是一個簡單的數據容器對象。實際數據(即圖表系列)是在別處創建的,並在構建器(模式)類中與圖表合併。所以我認爲我們有單一的責任模式受到尊重。 – gremo 2012-01-08 11:47:08