$this has no meaning outside of an object. You should only use $this and the -> operator when working with objects. -> is used for accessing the methods of an object and $this is used when an object needs to refer to itself. In this case there is no object at all so $this does not exist.
Here's an example of when you should use $this and ->
PHP Code:
class myObject
{
private $myVar;
function myFunc()
{
$this->myVar = 'hello world';
}
}
In this case you don't have to use $this, but it works either way. Most of the time when I use $this is when I'm hiding a variable (ie if myFunc to a variable called $myVar as input), but there are other contexts where it is useful as well.
By the way, is there a term for the -> operator? I usually just call it the arrow operator.
Last edited by NullPointer : 05-17-2008 at 12:48 PM.
|