Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 776
openfrog Avatar asked Dec 22 '09 18:12

openfrog


People also ask

What is difference between self and this in PHP?

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.

What is use of this -> data in PHP?

Since PHP can evaluate member names dynamically, the line $this->$_data. refers to a class member, which name is specified in local $data variable.

What is difference between self and static in PHP?

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.

What is :: VS --> in PHP?

Simply put, :: is for class-level properties, and -> is for object-level properties.

What is the difference between $this and $self in PHP?

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.

What is the use of self and this keyword in PHP?

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 ]

What is the difference between an object and a class in PHP?

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.

What is self in phpphp?

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.


Video Answer


1 Answers

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

like image 168
Tor Valamo Avatar answered Nov 01 '22 16:11

Tor Valamo