【发布时间】:2013-08-01 23:48:41
【问题描述】:
所以我了解如何链接方法,但我对链接变量感兴趣。这可以在PHP中做到吗?以下是我设想的用法示例:
Class Dog {
public $color; (Parent color)
public $eyes; (Child of color)
public $coat; (Child of color)
//Initialize dog
function __construct($color_eyes, $color_coat){
$this->color->eyes = $color_eyes;
$this->color->coat = $color_coat;
}
//Methods here, etc
function bark(){}
function walk(){}
}
如您所见,我希望链接的示例代码中的两个变量是:
$this->color->eyes
$this->color->coat
但我不确定如何实现变量链接,也不知道这是否可行。我之所以要这样做是因为我喜欢将所有内容分类以进行维护。
【问题讨论】:
-
想想吧!!
$this->color->eyes的结果是什么 -
我的问题是,我可以将 $this->color->eyes 设为合法变量,而不是 $this->color()->eyes 吗?在这种情况下,我希望颜色成为外套和眼睛的父母。