0

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?

5
  • Honestly - I don't think Description should be a sub-type of Name anyway; it's a different "thing". Commented Mar 8, 2018 at 9:09
  • @CD001 I'm learning this so it doesn't really matter if it's logic as long as the syntax is correct! Commented Mar 8, 2018 at 9:11
  • Fair enough - but if you're learning you may as well know when to sub-type a class - a sub-type should be a more specialized version of a type; Pug could be a sub-type of Dog which could be a sub-type of Mammal which could be a sub-type of Vertebrate ... and so on. That way new Pug() inherits the properties of Dog, Mammal and Vertebrate. Commented Mar 8, 2018 at 9:16
  • @CD001That's absolutely true.... Thnx tho' I now understand that to get the value I have to name the object I'm working in and execute the function of a parent Commented Mar 8, 2018 at 9:22
  • OOP is all about interfaces. What is the interface that your two classes share? Answer is that the baseclass implicitly defines the common interface. Now, think about what you can do with a Description instance 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. Commented Mar 8, 2018 at 9:23

3 Answers 3

4

In your case $name is completely unrelated to $description. However, this is easily fixed:

for ($i=0; $i < count($firstNames); $i++){
    $description = new Description();
    $description->setName($firstNames[$i], $lastNames[$i]);
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}

Bonus improvement:

foreach ($firstNames as $i => $firstName){
    $description = new Description();
    $description->setName($firstName, $lastNames[$i]);
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you.. I now see that $description can use the function of Name. However can you explain why your second option is an improvement?
I'd also move the $description = new Description() and put it before the loop begins. makes things faster. If OP has lots of logic in the __construct() function for example, it could make things slower
I understood that it would be faster and saver to build a new object for every instance.
@Interactive, this is highly subjective, but I think most folks consider foreach loops to be a bit more readable than for loops.
2
for ($i=0; $i < count($firstNames); $i++){
    $description = new Description();
    $description->setName($firstNames[$i], $lastNames[$i]);
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}

Comments

0

You are setting the name to another instance ( $name ) in order to set the name to the description object you have to write:

for ($i=0; $i < count($firstNames); $i++){

    $description = new Description();
    $description->setName($firstNames[$i], $lastNames[$i]);
    $description->setDescription($descriptions[$i]);

    echo $description->getDescription();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.