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

General Discussion

PHP constructor 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) { // error here , i dont know what time of error $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();

?>

Kazimierz Matan
Kazimierz Matan
13,257 Points

The best way of displaying your code is to put it between two sets of 3 backticks: (```) as described in Markdown Cheatsheet - link below editing window.

8 Answers

Kazimierz Matan
Kazimierz Matan
13,257 Points

Does this looks like your code in Notepad++?

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

?>

I copied this code to Notepad++, saved file and ran on XAMPP server. There were errors with this line:

$p= new product(); 

You created a new instance of Product class (use Product, not product) without parameters specified in constructor. You have to add these parameteres or comment out this line.

Both $shirt and $soda objects works OK.

can you send me the corrected code so i can run it. please

Kazimierz Matan
Kazimierz Matan
13,257 Points

Just copy and paste from my posting. And then add parameters to $p= new product(); or comment this line out

what did you mean to run it between two sets of 3 backticks: (```), i dont get that .

did i miss any parenthesis?

Kazimierz Matan
Kazimierz Matan
13,257 Points

3 backtits are used in this forum to mark start and end of posted code. Paranthesis are obsolete.

i already made those changes and it s still not working in XAmpp server

here is the new one i just tried after adding the changes to it

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

?>

Kazimierz Matan
Kazimierz Matan
13,257 Points
<?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();

?>

On my XAMPP server above code works and it outputs: "product name: Space Juice T-shirt".

Did you get any errors while trying to run this script? Is your XAMPP working? Do other script run well?

yes i kept getting errors

Kazimierz Matan
Kazimierz Matan
13,257 Points

So post these errors here. Errors are to help you know what is the problem.

Thanks it works fine now.