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 Static Methods and Properties

i have the problem in the code , but i do not see it

In the video we write the code. I have the problem because my code show this : "Product name: default_nameFlavor:" but in the course need : "Product name: Space Juice Soda Flavor: Grape "; maby because i am heedless ;-)

class Product 
{

    public $name = 'default_name';
    public $price = 0;
    public $desc = 'default decripion';

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

    }

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

    }

}

class Soda extends Product
{

    public $flavor;

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

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

$shirt = new Product("Space Juice T-Shirt", 20, "Awesome T-Shirt");
$soda = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape");

echo $soda->getInfo();
Marco Boretto
Marco Boretto
29,821 Points

I can see this: the method construct is called in the wrong way: first, the function should be called __construct($property1,$property2,$property3,$new_property); then, when you call the parent class with the parent key parent:: you should write it the same way as above

1 Answer

I see an underscore missing from the construct except the call to the parent. Should be 2 underscores like __construct. And parent construct is misspelled.

class Product {

public $name = 'default_name';
public $price = 0;
public $desc = 'default decripion';

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

}

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

}

}

class Soda extends Product {

public $flavor;

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;
}

}

$shirt = new Product("Space Juice T-Shirt", 20, "Awesome T-Shirt"); 
$soda = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape");

echo $soda->getInfo();