Where's the difference between self and $this-> in a PHP class or PHP method?
Solution 1
$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.
Solution 2
$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.
Solution 3
self
is used at the class-level scope whereas $this
is used at the instance-level scope.
Solution 4
- this-> can't access static method or static attribute , we use self to access them.
$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
Solution 5
self
refers to the calling object's class. $this
refers to the object itself.
openfrog
Thanks a lot to Pascal MARTIN, Gumbo and Quassnoi for their great help. Also many thanks to everyone else answering my questions.
Updated on July 09, 2022Comments
-
openfrog almost 2 years
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? -
AnrDaemon over 8 yearsself:: 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.
-
Tor Valamo over 8 years@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.
-
DarkWingDuck over 6 years@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.
-
Naz over 2 yearsoh that make sense! Thank you mate!