...

View Full Version : $this ->



USB
07-31-2012, 10:18 PM
hi, I wonder if $this is a already created variable in PHP and what " -> " is and when you use it.

I see that its used alot in this code.


function Login()

{


if(empty($_POST['username']))

{


$this->HandleError("UserName is empty!");

return false;

}


if(empty($_POST['password']))


{



$this->HandleError("Password is empty!");


return false;


}


$username = trim($_POST['username']);

$password = trim($_POST['password']);


if(!$this->CheckLoginInDB($username,$password))


{

return false;

}

session_start();

$_SESSION[$this->GetLoginSessionVar()] = $username;

return true;

}

Fou-Lu
07-31-2012, 11:45 PM
$this is context locked to a Class. You cannot create a variable called $this in any scope in PHP (can't recall how early back that goes; I believe late 4.2+ versions also did not allow this, but I think earlier versions did). It is automatically created as it refers to "this" object regardless of what instance "this" object is.

-> is the dereference operator. It is required to resolve "this" to the proper memory space to execute the given command. It is also required in PHP within any manipulation of a property or method since PHP does not support variable masking in its methods.

Effectively its purpose is to not mix up the instance of the class in question. If you have 2x instances of class Animal, and you modify one instance it will not modify the other instance (unless the property is static in which case its supposed to be shared between any instance of the class).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum