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 Object Inheritance

Drew Butcher
Drew Butcher
33,160 Points

Default values not appearing

Using this code if i echo $p->getInfo(); all the variable (i.e. properties) are blank. Why do the default values not show up?

3 Answers

Hello,

Did you instantiate the class? That is how you add the values to the member variables of the class. There are no default values to start with until you instantiate the class and pass the values in through the constructor. Hope this helped you.

Cheers!

Drew Butcher
Drew Butcher
33,160 Points

I don't know what that means. I just followed the code given in the video and I wanted to see what $p-getInfo(); would produce and I was surprised that it $name did not have the value of 'default_name'.... If you want to see my code here it is:

<?php
class Product
{
    // properties
    public $name = 'default_name'; // same as "var $name;"
    public $price = 0;
    public $description = 'default description';

    // methods
    function __construct($name, $price, $description){
        $this->name = $name;
        $this->price = $price;
        $this->description = $description;
    }

    public function getInfo(){
        return "<pre>
       Product Name: $this->name
      Product Price: $this->price
Product Description: $this->description</pre>";
    }
}

class Soda extends Product
{
    public $flavor;

    function __construct($name, $price, $description, $flavor){
        parent::__construct($name, $price, $description);
        $this->flavor = $flavor;
    }
    public function getInfo(){
        return "<pre>
       Product Name: $this->name
      Product Price: $this->price
     Product Flavor: $this->flavor
Product Description: $this->description</pre>";
    }
}

class Shirt extends Product 
{
    public $size;

    function __construct($name, $price, $description, $size){
        parent::__construct($name, $price, $description);
        $this->size = $size;
    }
    public function getInfo(){
        return "<pre>
       Product Name: $this->name
      Product Price: $this->price
       Product Size: $this->size
Product Description: $this->description</pre>";
    }
}

?>
<!DOCTYPE html>
<html>
<head>
    <title>Objects</title>
    <link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body style="background: gray;">
    <div class="phpoutput">
        <?php
            $p = new Product();
            $shirt = new Shirt("Space Juice T-Shirt", 20, "Awesome Gray T-Shirt", "Small");
            $soda = new Soda("Space Juice Soda", 2, "Thirst Mutilator", "Grape");
            echo $p->getInfo();

        ?>
    </div>
</body>
</html>
Drew Butcher
Drew Butcher
33,160 Points

Hey Shawn Gregory it looks like I did at the end of the code where it says: "$p = new Product();" but still nothing appears!

Drew,

Your problem stems from not providing arguments to your product instantiation. While specifying arguments to a constructor of a class is optional, if you don't specify an argument, PHP will put a NULL value in its place. Your default values are being overwritten by those NULL values and therefor are not displaying anything. Add the arguments and it will display the values you specified. If you just add the name argument and nothing else you'll see that when the page is displayed, only the name will appear.

Default values for member variables are used when you may or may not replace it with anything during run-time and if you do, you will most likely add a conditional statement that either changes it to the new value or leave the default value. If you want it to display the default value if you supply no argument to the instantiation, use the following modifications:

function __construct($name, $price, $description){
  if(isset($name)) $this->name = $name;
  if(isset($price)) $this->price = $price;
  if(isset($description)) $this->description = $description;
}

This way if you supply no arguments or only one or two, the default values you had in the class will display. Try it out and tell me the results.

Cheers!