PHP $this Keyword
If you are following this tutorial from the beginning or you started from the OOPS concepts, you must have noticed the usage of $this in some of the code snippets.
this keyword is used inside a class, generally withing the member functions to access non-static members of a class(variables or functions) for the current object.
Let's take an example to understand the usage of $this.
<?php
class Person {
// first name of person
private $name;
// public function to set value for name (setter method)
public function setName($name) {
$this->name = $name;
}
// public function to get value of name (getter method)
public function getName() {
return $this->name;
}
}
// creating class object
$john = new Person();
// calling the public function to set fname
$john->setName("John Wick");
// getting the value of the name variable
echo "My name is " . $john->getName();
?>
My name is John Wick
In the program above, we have created a private variable in the class with name $name and we have two public methods setName() and getName() to assign a new value to $name variable and to get its value respectively.
Whenever we want to call any variable of class from inside a member function, we use $this to point to the current object which holds the variable.
We can also use $this to call one member function of a class inside another member function.
NOTE: If there is any static member function or variable in the class, we cannot refer it using the $this.
Using self for static Class Members
Instead of $this, for static class members(variables or functions), we use self, along with scope resolution operator ::. Let's take an example,
<?php
class Person {
// first name of person
public static $name;
// public function to get value of name (getter method)
public static function getName() {
return self::$name; // using self here
}
}
?>
Difference between PHP self and this
Let's understand a few differences between self and this:
self | this |
self keyword is not preceded by any symbol. | this keyword should be preceded with a $ symbol. |
To access class variables and methods using the self keyword, we use the scope resolution operator :: | In case of this operator, we use the -< symbol. |
It is used to refer the static members of the class. | 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. |
Let's take a code example to understand this better:
<?php
class Job {
// opening for position
public $name;
// description for the job;
public $desc;
// company name - as the company name stays the same
public static $company;
// public function to get job name
public function getName() {
return $this->name;
}
// public function to get job description
public function getDesc() {
return $this->desc;
}
// static function to get the company name
public static function getCompany() {
return self::$company;
}
// non-static function to get the company name
public function getCompany_nonStatic() {
return self::getCompany();
}
}
$objJob = new Job();
// setting values to non-static variables
$objJob->name = "Data Scientist";
$objJob->desc = "You must know Data Science";
/*
setting value for static variable.
done using the class name
*/
Job::$company = "Studytonight";
// calling the methods
echo "Job Name: " .$objJob->getName()."<br/>";
echo "Job Description: " .$objJob->getDesc()."<br/>";
echo "Company Name: " .Job::getCompany_nonStatic();
?>
Job Name: Data Scientist
Job Description: You must know Data Science
Company Name: Studytonight
In the code snippet above we have a few non-static variables and one static variable.
Because the static members are associated with class itself and not the objects of the class, hence we call them using the class name.
Also, a static member function can use a static variable inside it, while if a non-static method use a static variable inside it, then it is also called using the class name, just like a static method.