Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
Thread: $this ->
07-31-2012, 11:18 PM #1
- Join Date
- Jan 2012
- Thanked 0 Times in 0 Posts
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.
$this->HandleError("UserName is empty!");
$this->HandleError("Password is empty!");
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$_SESSION[$this->GetLoginSessionVar()] = $username;
Last edited by USB; 07-31-2012 at 11:24 PM.
08-01-2012, 12:45 AM #2
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,662 Times in 2,631 Posts
$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).