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