所以我有这个代码,见下文。我在获取受保护的值时运行错误。认为受保护的值将被继承。我正在弄清楚为什么我们需要调用parent::__construct()
,而我们只能扩展一个类。
如果你能告诉我什么是错的,我怎样才能以正确的方式做到这一点,那就太棒了。
<?php
/**
* DogWords
*/
class DogWords
{
protected $words = array('Wrr', 'Grr', 'Weeee', 'Houou');
}
/**
* Dog Class
*/
class Dog extends DogWords
{
protected $dogname = NULL;
protected $dogwords = NULL;
public function __construct($dogname)
{
$this->dogname = $dogname;
$this->dogwords = new DogWords;
}
public function bark()
{
echo $this->dogname . "; Bark, bark, bark...";
}
}
/**
* Poodle
*/
class Poodle extends Dog
{
}
$Amy = new Poodle('DogConstructor');
echo $Amy->dogwords->words[1]; // Fatal Error...
echo $Amy->bark(); // DogConstructor; Bark, bark, bark...
?>