So I have this code, see below. I am running an error while getting a protected value. Thought that protected values will be inherited. I was figuring out why we need to call parent::__construct(), while we just can extend a class.
If you could tell me whats wrong and how can I do it the right way, that would be awesome.
<?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...
?>
DogextendDogWords. That's just silly.<name>Words. Having an abstract class which has a words property is one thing, having a dog be a subclass ofbarkis just wrong.