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

constructor function issue

i am using notepad ++, everything was working fine until i added the constructor to it.

function __construct($name, $price, $desc) { $this->name = $name; $this->price = $price; $this->desc = $desc; } // the following is my entire code. the constructor does not work. i have no clue why it does not work. // everything was working fine until i added the constructor <?php class Product { public $name ='default_name'; public $price = 0; public $desc ='default description'; //constructor function __construct($name, $price, $desc) { $this->name = $name; $this->price = $price; $this->desc = $desc; }

//$p= new product(); //$p->name=" Space Juicie "; //echo $p->name; public function getInfo(){ // public method

return "product name: ". $this->name; } } $p= new product(); //echo $p->getInfo(); $shirt = new Product("Space Juice T-shirt", 20, "Awsome Grey T-shirt"); $soda = new Product("Space Juice Soda", 2, "Grape Flavored Thirst Mutilator"); echo $shirt->getInfo(); //echo $soda->getInfo();


Gareth Borcherds
Gareth Borcherds
9,372 Points

Can you post more of your code? Like where is this construct being put? It might help to narrow it down more

1 Answer

$p= new product(); here you have to add the parameter from your constructor try

$p= new product('Space Juicie', 10, 'desc example');