Hi,
In PHP and most other languages that I know of, objects are merely an aggregation of data (variables) in memory. Consider this example:
PHP Code:
class Person
{
var $firstName;
var $lastName;
function printName()
{
echo $this->firstName . " " . $this->lastName;
}
function Person($firstName, $lastName)
{
$this->firstName = $firstName;
$this->lastName = $lastName;
}
};
$johnSmith = new Person('John', 'Smith');
$janeDoe = new Person('Jane', 'Doe');
$johnSmith->printName(); /* Print: John Smith */
$janeDoe->printName(); /* Print: Jane Doe */
When you create an object of type Person ($johnSmith), PHP allocates memory space for two variables, $firstName and $lastName (and some metadata too, but this is not relevant). Both $johnSmith and $janeDoe have their own first and last names, but that's all. The functions themselves are not copied however, and only one instance of each function is shared for all Person objects. Here is a simplified snapshot of what the memory could contain:
["John" "Smith" ... "Jane" "Doe" ... [Shared code for printName] ...]
Thus, later, when the printName function is called, it has no way to know by itself whether the call comes from $johnSmith or $janeDoe (i.e. it has no clue which object it "belongs" to). Thankfully, PHP maintains the special variable $this that holds a reference to the object that made the call. This provides you with a way to reference any of the other class variables or functions that "belong" to the current object. Essentially, $this is a hint that tells PHP where to find the current object, and $this->firstName is a way to say "go get my first name."
Some other languages will do this automatically without you needing to reference the $this keyword, but PHP does not. Basically, always use $this-> when referring to anything that belongs to a specific object.