Reputation: 5714
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:
greet()
method in question A & B is correct?
2.The constructor
and related properties
inside the sub class
for question B, I have my doubts whether those are correctly coded...even though I'm getting correct output, Please see comments in codeAny 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');
Upvotes: 1
Views: 1955
Reputation: 570
Another way of doing this
<?php
class Animal{
protected $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();
class Dog extends Animal
{
private $animalType;
public function __construct($animalType,$dogname){
$this->animalType = $animalType;
parent::__construct($dogname);
}
public function greet(){
echo "Hello, I'm a ".$this->animalType. " and my name is ".$this->name;
}
}
$dog = new Dog('Dog', 'New Dog');
echo $dog->greet();
?>
Upvotes: 0
Reputation: 1857
Problems with your code:
1) Why would you have Dog::$animalType
field? The purpose of extending generic Animal
class is to make it rather specific with Dog
subclass. Instead, just use strtolower(__CLASS__)
for animal type.
2)Dog::greet()
method doesn't need any arguments. It can clearly use $this->dogName
.
3) You should name both name fields as Animal::$name
and Dog::$name
. That way, the sub-class will inherit from its parent class and override the values. Instead you have introduced additional field which does the same thing, and breaks the logical relation between parent and child classes.
4) Both parent and child classes should have same signatures for methods with the same name, so that inheritance could have a purpose. And since child classes inherit parent's classes as they are, there is no need to re-define them (in your case, the constructor).
class Animal
{
private $name; // can't be private (unless you use a getter), if you want to extend it
public function __construct($name)
{
$this->name = $name;
}
public function greet()
{
return "Hello I'm some sort of animal and my name is " . $this->getName();
}
protected function getName()
{
return $this->name;
}
}
class Dog extends Animal
{
public function greet()
{
return "Hello, I'm a " . strtolower(__CLASS__) . " and my name is " . $this->getName();
}
}
$animal = new Animal('Cow');
echo $animal->greet();
$dog = new Dog('Max');
echo $dog->greet();
Upvotes: 1
Reputation: 106
I think you don't need the constructer in Dog class. The greet function looks wrong. I think you have to use the name from Animal class by using a name getter (getName).
class Animal
{
private $name;
public function __construct($name)
{
$this->name = $name;
}//construct
public function greet()
{
$sting = "Hello I'm some sort of animal and my name is " . $this->getName();
return $sting;
}//function
public function getName() {
return $this->name;
}
}//class
$animal = new Animal('Jock');
echo $animal->greet();
class Dog extends Animal
{
public function greet()
{
$string = "Hello, I'm a dog and my name is " . $this->getName();
return $string;
}
}
$dog = new Dog('Jock');
echo $dog->greet();
Upvotes: 1
Reputation: 1558
I think that the question is worded slightly incorrectly. I think that it wants you make the properties protected
rather than private
to extend the class (otherwise you would have to re-define the same properties in the subclass).
If this is the case, here is an example that shows class inheritance:
class Animal
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function greet()
{
return "Hello I'm some sort of animal and my name is {$this->name}.";
}
}
$animal = new Animal('Jock');
echo $animal->greet();
class Dog extends Animal
{
public function greet()
{
return "Hello I'm a dog and my name is {$this->name}.";
}
}
$dog = new Dog('Jock');
echo $dog->greet();
You can see that we don't need to redefine the property $name
and the constructor. The class Dog
knows it's a dog, so we don't need to tell it that - we just adjust our greet method accordingly. This shows how we can access the protected
property $name
from Dog
, and shows how we have overridden the greet
method in Dog
.
I hope this sheds some light on the subject.
Upvotes: 1