22

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?

Lukáš Lalinský
  • 40,587
  • 6
  • 104
  • 126
openfrog
  • 40,201
  • 65
  • 225
  • 373

7 Answers7

20

$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.

Tor Valamo
  • 33,261
  • 11
  • 73
  • 81
  • self:: inside an object refers to the same inheritance level properties/methods, where $this-> refers to the object inherited methods. In other words, self:: is more precise. And inside an initialized object, it is not a static call, either, unless you address an explicitly defined static properties/methods. This "answer" is wrong from top to bottom. – AnrDaemon Feb 10 '16 at 07:10
  • @AnrDaemon I don't know how old you are, but before you got your degree in hindsight, this answer was 100% correct. I don't know what PHP has done later, since I quit using it when the developers began demonstrating a blatant disregard of quality or empirical data to make their decisions. That was probably before this answer. Also this answer holds true in every other object oriented language with respect for itself. If it doesn't in PHP, then PHP is wrong, not me. Thank you for your input, it has been noted and discarded. – Tor Valamo Feb 13 '16 at 23:24
  • @TorValamo no, your answer is right. The commenter probably was confused by the usage of 'this' in JavaScript, which keeps the lookup within the current context. – DarkWingDuck Nov 23 '17 at 11:40
19

$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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Yacoby
  • 54,544
  • 15
  • 116
  • 120
4

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

jldupont
  • 93,734
  • 56
  • 203
  • 318
4
  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.

    <?php
    class FirstClass{
      function selfTest(){
        $this->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
    
Pang
  • 9,564
  • 146
  • 81
  • 122
Ahmed Magdy
  • 101
  • 4
2

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

Matchu
  • 83,922
  • 18
  • 153
  • 160
2

$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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
2

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

Nimatullah Razmjo
  • 1,831
  • 4
  • 22
  • 39