tags:

views:

556

answers:

8

I am using 'this' keyword for a long time. But when someone asks me to explain it, I am confused that how to explain it. I know that I can use this in a method of class to access any variable and method of the same class.

    class MyClass{

      function MyMethod1(){
        echo "Hello World";
      }

      function MyMethod2(){
        $this->MyMethod1();
      }

    }

Is it a object of a class that we don't need to initialise and can be used only within the class or anything else. How to explain?

Thanks

+2  A: 

this references the current object instance of a class.

this is an implicitly parameter passed to the methods of a class: it is scoped to a method and allows access to all of the object's members.

jldupont
Probably not basic enough, but this is the most correct answer technically.
David Pfeffer
+1  A: 

Here is a nice article on this

Using the keyword "this" in PHP

rahul
+5  A: 

A class is a mold for an object: it specifies how the object looks like (variables) and what it can do (functions).

If you instanciate a class: you create an object. If you create the class, you can use "this" to refer to the object itsself. This is why you can't set the "this", because it's related to the object. It's a special, read-only variable.

Pindatjuh
It's a special, read-only variable that can be used to refer to the object itself. Is it correct ?
NAVEED
Yes, it is correct.
Pindatjuh
+1  A: 

short: $this gives you access to the object variables (and methods) Edit: within the class :) Edit 2: (but not in static methods of the class) :D

dfens
within the class :)
NAVEED
corrected;) thx mate
dfens
but not in static methods of the class :))
NAVEED
of course, corrected again :)
dfens
You can use self (and parent and static) in static methods ;-) http://de.php.net/manual/en/language.oop5.static.php
Thomas
@Thomas: its interesting and helpful.
NAVEED
+1  A: 

Like their name suggests, instance methods operate on instances of a class. How do they know which one to operate on? That's what the this parameter is for.

When you invoke an instance method, you're really invisibly passing in an extra parameter: the object to invoke it on. For example, when you have this:

class Basket {
  public function a() {
    $this-> ...;
    // ...
  }
  // ...
}

and you call $some_basket->a(), behind the scenes you're actually calling something like Basket::a($some_basket). Now a() knows which Basket you want to work with. That special parameter is what this refers to: the current object you're dealing with.

John Feminella
A: 

you can explain it by substituting the 'this' with the class name. for example, if the class name is 'Book' and the variable name is 'page', then 'this.page' can be explained as 'Book.page'. But of course, this is good for explanation purpose only

Hery
+1  A: 

A class is a template or a 'die' for an object.

Lets use the classic 'bicycle' example. There are many huffy bikes out there. However, we have created one bike, and we can use the 'this' keyword to refer to 'this' bike.

In more a more technical sense, a class is a template for an object that will be instantiated. At run time, after an object has been instantiated, or had an instance of itself created, we can then use the keyword 'this' internally to refer to the instance that runs that method.

JC
+1  A: 

Several people have explained it in similar terms, but thought I'd add that when speaking to people unfamiliar with object oriented programming, I explain that the class definition is the blueprint, as for a house, and "this" is the actual house you're working with at that moment. There might be other houses that look exactly the same, but this is the specific object (house).