Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: $this ->

  1. #1
    USB
    USB is offline
    New Coder
    Join Date
    Jan 2012
    Posts
    29
    Thanks
    5
    Thanked 0 Times in 0 Posts

    $this ->

    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.
    PHP 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;


    Last edited by USB; 07-31-2012 at 10:24 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •