SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast QuaffAPint's Avatar
    Join Date
    Feb 2002
    Location
    West Chester, PA
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Classes and $this

    Ok - I'm just not making it click....

    I've read a few articles on Classes in PHP4, and I'm trying to understand the '$this' variable.

    Could someone please try to get this through my thick head, of what exactly this variable is attempting to accomplish in a Class...

    Thank you.
    QuaffAPint

  2. #2
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The basis of understanding the $this variable is understanding the difference between a class and an object: a class is a blueprint, or template, for as many objects (of that class) as you like.

    Imagine you have a class Car, implemented (partly) as follows:

    PHP Code:
    class Car
    {
      var 
    $speed;
      var 
    $topspeed;
      
      function 
    Car($topspeed)
      {
        
    $this->topspeed topspeed;
        
    $this->speed 0;
      }

      function 
    accelerate()
      {
        if (
    $this->speed $this->topspeed)
        {
          
    $this->speed++;
        }

    Now imagine you have two objects of the class above (remember: you can have as many at the same time as you'd like!): $car1 and $car2. They were created like this:

    PHP Code:
    $car1 = new Car(90);
    $car2 = new Car(100); 
    Thus, the first car has a topspeed of 90 mph, while the second can go a bit faster, at 100 mph.

    When I call the class-method 'accelerate', how does my class (the template) now how fast it can go? It depends on the actual car I'm sitting in. That is what the $this variable is for. You can use it inside a class to get access to the actual object that is calling the method, so that it can differentiate between all objects that can exist at the same time. By calling '$car1->accelerate()', the class-method 'accelerate' can get information about that particular car by following the $this-pointer. $this->topspeed will result in '90', while it will result in '100' when I call it on $car2.

    Hope this helps!

    Vincent

  3. #3
    purple monkey dishwasher scoates's Avatar
    Join Date
    Nov 2001
    Location
    Montreal
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If my understanding is correct, $this is nothing more than a reference to the currently executing instance of an object.

    If you have an object named Fruit($type) and you instanciate it using $apple = Fruit('apple'). Then any methods within that instance of Fruit that want to reference $apple can use $this. So, for the above instance of Fruit, it's method slice() could call $this->type and get "apple".

    Sorry, that's probably not very clear. Feel free to ignore me (-;

    S

  4. #4
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yup $this is a reference to the currently existing (or executing) object... something like 'self' if you're familiar with that syntax...

    $this is used mostly in class functions where you have to access the object's variables or functions and be sure of the scope... the only way to fully qualify where the particular class variable or function is coming from is thru $this->var or $this->myfunction(), since the object itself has no name that the function would know of...

  5. #5
    SitePoint Enthusiast QuaffAPint's Avatar
    Join Date
    Feb 2002
    Location
    West Chester, PA
    Posts
    91
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, all. I'm going to print this thread out and just let it soak in for awhile.

    Hopefully the lightbulb will go off soon enough...

    -QuaffAPint


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
  •