What is difference between this and self in php?

Where's the difference between self and $this-> in a PHP class or PHP method?

Example:

I've seen this code recently.

public static function getInstance() {

    if (!self::$instance) {
        self::$instance = new PDO("mysql:host='localhost';dbname='animals'", 'username', 'password');;
        self::$instance-> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    return self::$instance;
}

But I remember that $this-> refers to the current instance (object) of a class (might also be wrong). However, what's the difference?

What is difference between this and self in php?

asked Dec 22, 2009 at 18:37

What is difference between this and self in php?

3

$this refers to the instance of the class, that is correct. However, there is also something called static state, which is the same for all instances of that class. self:: is the accessor for those attributes and functions.

Also, you cannot normally access an instance member from a static method. Meaning, you cannot do

static function something($x) {
  $this->that = $x;
}

because the static method would not know which instance you are referring to.

answered Dec 22, 2009 at 18:40

What is difference between this and self in php?

Tor ValamoTor Valamo

32.5k11 gold badges71 silver badges81 bronze badges

3

$this refers to the current object, self refers to the current class. The class is the blueprint of the object. So you define a class, but you construct objects.

So in other words, use self for static and this for non-static members or methods.

What is difference between this and self in php?

mickmackusa

39k11 gold badges76 silver badges112 bronze badges

answered Dec 22, 2009 at 18:40

YacobyYacoby

53.3k13 gold badges111 silver badges119 bronze badges

self is used at the class-level scope whereas $this is used at the instance-level scope.

answered Dec 22, 2009 at 18:39

jldupontjldupont

89.6k56 gold badges195 silver badges310 bronze badges

  1. this-> can't access static method or static attribute , we use self to access them.
  2. $this-> when dealing with extended class will refer to the current scope that u extended , self will always refer to the parent class because its doesn't need instance to access class method or attr its access the class directly.

    classCheck();
        self::classCheck();
      } 
      function classCheck(){
        echo "First Class";
      }
    }
    class SecondClass extends FirstClass{
        function classCheck(){
          echo "Second Class";
        }
    }
    $var = new SecondClass();
    $var->selfTest(); //this-> will refer to Second Class , where self refer to the parent class
    

What is difference between this and self in php?

Pang

9,192146 gold badges85 silver badges118 bronze badges

answered May 18, 2017 at 4:08

What is difference between this and self in php?

self refers to the calling object's class. $this refers to the object itself.

answered Dec 22, 2009 at 18:39

MatchuMatchu

81.9k17 gold badges151 silver badges160 bronze badges

1

$this is used to reference methods and properties of the current instance of a class.

self us used to reference static methods and properties, shared by all instances (and even accessible outside of any instance) of a class.


You can take a look at Static Keyword (quoting a few lines) :

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can)

...

Static properties cannot be accessed through the object using the arrow operator ->.


And, from the page Properties (quoting) :

Within class methods the properties, constants, and methods may be accessed by using the form $this->property (where property is the name of the property) unless the access is to a static property within the context of a static class method, in which case it is accessed using the form self::$property.

answered Dec 22, 2009 at 18:42

Pascal MARTINPascal MARTIN

388k77 gold badges646 silver badges656 bronze badges

$this is use to call the instance of class, where self:: is mostly used to call the constant variable within class.

answered Jun 8, 2015 at 8:55

What is difference between this and self in php?

Is self and this keyword same?

Technically both self and this are used for the same thing. They are used to access the variable associated with the current instance. Only difference is, you have to include self explicitly as first parameter to an instance method in Python, whereas this is not the case with Java.

What self means PHP?

PHP self refers to the class members, but not for any particular object. This is because the static members(variables or functions) are class members shared by all the objecxts of the class. Whereas, $this wil refer the member variables and function for a particular instance.

What is the use of this in PHP?

$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.

What is the purpose of and PHP self?

In PHP, the self and this keyword are used to refer to class members within the scope of a class. The class members can be either variables or functions. These PHP keywords differ from the static behavior of the class members.