Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Chris Shearon
PLUS
Chris Shearon
Courses Plus Student 14,369 Points

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();