Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:链接类变量(不是方法)_Php_Variables_Chaining - Fatal编程技术网

PHP:链接类变量(不是方法)

PHP:链接类变量(不是方法),php,variables,chaining,Php,Variables,Chaining,所以我了解如何链接方法,但我对链接变量感兴趣。这可以在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

所以我了解如何链接方法,但我对链接变量感兴趣。这可以在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

但我不确定如何实现变量链接,也不知道这是否可行。我之所以要这样做,是因为我喜欢为维护目的对所有内容进行分类。

不是在一个类中,不是

顺便说一句,考虑类和继承的逻辑方式是有颜色的眼睛,而不是有颜色的眼睛!因为你有一门狗课,狗有眼睛,狗的眼睛有颜色


但是,如果创建一个名为Color的基类,然后创建一个扩展Color类的名为Eyes的类,然后创建一个扩展Color类的名为Coat的类,则可以实现类似的功能。

想想看!!那么,
$this->color->eyes
的结果会是什么呢?我的问题是,我能让$this->color->eyes成为合法变量,而不是$this->color()->eyes吗?在这种情况下,我想颜色是父母的大衣和眼睛。