Im taking a 3rd year module on webDesign focusing on OOP concepts which im struggling to master, but slowly getting there...
An Assignment Question Reads As Follows:
I coded thequestions above and I got the correct output but:
- I am unsure if the way I coded the
greet()method in question A & B is correct? 2.Theconstructorand relatedpropertiesinside thesub classfor question B, I have my doubts whether those are correctly coded...even though I'm getting correct output, Please see comments in code - I fail to see the benefit of extending the class, perhaps I don't see it because I Went about it the wrong way..?
Any advice appreciated, code follow below.
A)
class Animal{
private $name;
public function __construct($dogName){
$this->name = $dogName;
}//construct
public function greet(){
$sting = "Hello I'm some sort of animal and my name is .$this->name.";
return $sting;
}//function
}//class
$animal = new Animal('Jock');
echo $animal->greet();
B - SubClass
class Dog extends Animal
{
private $animalType;
public function __construct($animalType, $dogName){
$this->$animalType = $animalType;
$this->dogName = $dogName; //IS THIS LINE CORRECT, IF YES WHY SHOULD I USE IT AGAIN IN PARENT::_CONSTRUCT?
//Call Animal Constructor To Finish
parent::__construct($dogName);
}//constructor
public function greet($value1, $value2){
$this->animalType = $value1;
$this->dogName = $value2;
$string = "Hello, I'm a ". $value1. " and my name is ". $value2;
return $string;
}
}
$dog = new Dog('Dog', 'Jock');
echo $dog->greet('Dog', 'Jock');
