I'm trying OO in PHP. I think I understand what goes wrong but I don't understand how to fix it.
class Name {
public $_firstname;
public $_lastname;
public function setName($firstName, $lastName){
$this->_firstname = $firstName;
$this->_lastname = $lastName;
}
public function getName(){
echo 'The full name is '. $this->_firstname. ' ' . $this->_lastname .'<br>';
}
}
class Description extends Name{
public $_desciprion;
public function setDescription($description){
$this->_desciprion = $description;
}
public function getDescription(){
echo $this->_desciprion. ' is written by '.$this->_firstname .'<br>';
}
}
$firstNames = array("some", "another", "john");
$lastNames = array("body", "body", "doe");
$descriptions = array("description 1", "description 2", "description 3");
for ($i=0; $i < count($firstNames); $i++){
$name = new Name();
$name->setName($firstNames[$i], $lastNames[$i]);
$description = new Description();
$description->setDescription($descriptions[$i]);
echo $description->getDescription();
}
I want to echo the $description containing the $_firstname of the Name class.
I don't really know the OO way to fill it.
Thoughts?
Descriptionshould be a sub-type ofNameanyway; it's a different "thing".Pugcould be a sub-type ofDogwhich could be a sub-type ofMammalwhich could be a sub-type ofVertebrate... and so on. That waynew Pug()inherits the properties ofDog,MammalandVertebrate.Descriptioninstance if you only use the common interface? Answer is, that they are both completely indistinguishable, which is another indicator that you picked a bad example.