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

OOP PHP __construct

Please correct me if I am wrong. I have probably missed something basic. It seems to me that the purpose of __construct is to enable an object to modify a class'property. Give the following code examples:

<?php      
  class Product
  {
    public $name = 'default name';
    public $price = 0;
    public $desc = 'default description';

    function __construct($name, $price, $desc) {
      $this->name = $name;
      $this->price = $price;
      $this->desc = $desc;
    }

    public function getInfo() {
      return "Product Name: " . $this->name;
    }
  }

  $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 "<hr>";
  echo $soda->getInfo();
?>

The output from the above is:
Product Name: Space Juice T-Shirt
Product Name: Space Juice Soda

And the following code:

<?php      
  class Product
  {
    public $name = 'default name';
    public $price = 0;
    public $desc = 'default description';

    public function getInfo() {
      return "Product Name: " . $this->name;
    }
  }

  $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 "<hr>";
  echo $soda->getInfo();
?>

Outputs:
Product Name: default name
Product Name: default name

Is this all there is to __construct?

Jeff

1 Answer

No, the magic method __construct() is used to initialize an object. Whenever you make a new object using the "new" keyword, PHP automatically runs the __construct method. In the example above, yes, all the construct method does in set some properties, but in other classes the __construct() method can do more things. For example, the built in PDO class for PHP accepts a DNS, username, and password as arguments for its __construct method. When the __construct method is called by:

$db = new PDO("mysql:host=127.0.0.1;dbname=test", "root", "root");

PDO doesn't just set some properties, it also tries to connect to the database to ensure that the information you gave it is correct. Another example could be a url parser class for a site built using a front-controller/single point of entry. This class would be responsible for parsing all of the url after the .com into information the site can use to make choices about how to responsed to a request. Personally, inside the __construct method I would go ahead a run a method to break the url into it's parts instead of having to call it manually somewhere else.

Jeff, did that clear things up for you?

Hi Andrew,

I think so. So what you're saying is that often the constructor has other code in it besides defining properties (variables). It could also be performing functions and conditionals.

Jeff

Yes, the constructor is used for setting any variables and running any functions that you need to set up an object.