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 Constructor Method

$this in constructor

I am confused when $this->name = $name is used, because i cannot understand "name" used for reference, which element it is referring to? Is it the where we declared in start or function args?

everything withing the __construct helps you set the parammetters for your methods in your class. for example

<?php
class test {

public $name;

    function __construct($name) {
         $this->$name = $name
    }

    public function result {
            echo "Hello ". $name;
    }
}

$obj = new test;
// Because i defined the parammetters of my method in my __construct the following will print
// the echo message from my method "result" and set $name to my name.
$obj->result('Ram');
?>

In $this->name = $name , the first "name" is a property of the class, and the second "$name" is the variable value that you are assigning to it. $name could have been passed into the function, or declared within the class.