Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Object Inheritance

How did getInfo(); printed flavor?

While getInfo function has return value of $this->name; which is just name, how come when $soda->getInfo(); is asked, it prints flavor too, whereas in method, it is just $this-name

it would be more helpfull if you provide the code that you're trying to input

the values that you get back from a method are defined in your __construct when you call your method outside a class and put a flavor as a parammetter within the parentesis

the computer won't know if its a name or a flavor because all of what the computer is expecting is a string value.

hope this helps

1 Answer

Hi!

Your question is a bit hard to understand, due to there not being any code, but I'll do my best to give a (hopefully) helpful response. So, our class of Product has the method of getInfo() which returns the given name that you input. In our Soda class, we also have the getInfo() method, but this time it returns not only the name, but also the flavor.

The reason for this is that we are inheriting from our Product class and just adding onto it. Here's the Product class' methods:

  function __construct($name, $price, $desc){
    $this->name = $name;
    $this->price = $price;
    $this->desc = $desc;
  }

  public function getInfo(){
    return "Product Name: ". $this->name;
  }

If we were to create a new Product class, we would only be able to print out the name, unless we changed it, of course. Now, let's look at the methods for our Soda class which has been extended by our Product class:

  function __construct($name, $price, $desc, $flavor){
    parent::__construct($name, $price, $desc);
    $this->flavor = $flavor;
  }

  public function getInfo(){
    return "Product Name: " . $this->name . " Flavor: " . $this->flavor;
  }

As you can see, we have added onto our __construct method, which enables us to use $flavor in any Soda object. Instead of writing a whole mess of a new stuff, we can simply extend a class, which helps us stay DRY. If we create a new Soda object, we can pass in $flavor, whereas Product does not contain the $flavor parameter.

$soda = new Soda("Space Juice Soda", "5", "Thirst Mutilator", "Grape");

Hope this helped!