More specificaly, it's reference to variables and functions /inside/ the current object or class.
For example:
--------------------------
class AngryMonkey {
var $name;
var $colour;
var $has_tail;
function AngryMonkey(){
$name = "Alfred"; // INCORRECT: Sets the variable "name" that exists -only- inside the AngryMonkey constructor, this will cease to exist once AngryMonkey has been created
$this->name = "Alfred"; // CORRECT: This properly sets AngryMonkey::name to "Alfred"
$this->colour = "Red";
$this->has_tail = is_monkey($this->name); // INCORRECT: PHP will die, warning that the function is_monkey does not exist
$this->has_tail = $this->is_monkey($this->na
}
function is_monkey($name){
return ($name == "Alfred") ? true : false;
}
}
--------------------------
Forgive me if the example is a bit bizarre and any spelling mistakes there may be, I need more caffeine.
Main Topics
Browse All Topics





by: techtonikPosted on 2004-05-20 at 12:23:05ID: 11120736
It is reference to current object. Say you program a method in obect and need to access it's variable with name for example $namevar. Normally, when you define an instance of the object like.
en/languag e.oop.php
$objvar = new MyObject();
you access this variable like
$objvar->namevar = "whatever";
but when you access this variable from inside of an object, you don't know about object variable name - $objvar, so there is $this keyword which means literally "this object", so use $this->namevar to access variable from it's method.
http://us2.php.net/manual/