SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: This

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This

    Could someone give me a brief explanation of the "this" command, as I am utterly confused

    Thank you in advance for your response

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    There is no 'this' command.

    $this is a special variable inside of a class definition. $this is used to refer to the instance of the object implicitly, without needing to know the instantiation name.


    For example.

    PHP Code:
    class HelloWorld {
      public 
    $msg;

      function 
    __construct($in) {
        
    $this->msg $in;
      }

    So, i've got a class. Named HelloWorld. When I create an instance of my HelloWorld object, I have to specify a message. This message could be anything, and can be different for each instance of HelloWorld.

    PHP Code:
    $bunny = new HelloWorld("Bunnies are cool.");
    $lion = new HelloWorld("Bunnies are tasty."); 
    Note that the class definition has no idea that i've named my object variables $bunny and $lion... and it doesnt care, either.

    PHP Code:
    echo $lion->msg//Bunnies are tasty. 


Bookmarks

Posting Permissions

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