Reputation: 40735
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?
Upvotes: 22
Views: 25300
Reputation: 55445
$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.
Upvotes: 19
Reputation: 101
$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
Upvotes: 4
Reputation: 33749
$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.
Upvotes: 20
Reputation: 1961
$this is use to call the instance of class, where self:: is mostly used to call the constant variable within class.
Upvotes: 2
Reputation: 400952
$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
(whereproperty
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 formself::$property
.
Upvotes: 2
Reputation: 85794
self
refers to the calling object's class. $this
refers to the object itself.
Upvotes: 2
Reputation: 96716
self
is used at the class-level scope whereas $this
is used at the instance-level scope.
Upvotes: 4