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?
self is used to access static or class variables or methods and this is used to access non-static or object variables or methods. So use self when there is a need to access something which belongs to a class and use $this when there is a need to access a property belonging to the object of the class.
Since PHP can evaluate member names dynamically, the line $this->$_data. refers to a class member, which name is specified in local $data variable.
PHP new self vs new static: Now that we changed the code in our example to use static instead of self, you can see the difference is that self references the current class, whereas the static keyword allows the function to bind to the calling class at runtime.
Simply put, :: is for class-level properties, and -> is for object-level properties.
It is used to access non-static members of the class. 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.
In PHP, the self and this keyword are used to refer class members within the scope of a class. The class members can be either variables or functions. These PHP keywords differ with the static behavior of the class members. PHP this keyword refers a non-static member of a class with respect to the class ]
PHP supports classes and other object-oriented constructs. Static functions and variables in PHP classes are not associated with any specific instance of the class (in other words, an object). See difference between object and class. Instead, static functions and variables are associated with the class definition itself.
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.
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With