In PHP, the self and this keyword are used to refer to class members within the scope of a class. The class members can be either variables or functions. These PHP keywords differ from the static behavior of the class members.
PHP this keyword refers to a non-static member of a class with respect to the class instance created. So, the values or arguments of the class members will be varied based on the value with which the instance of the class is created.
If we use this keyword without object context for referring to static data, then the following error will be displayed to the browser.
Fatal error: Using $this when not in object context in ... on Line
So, we should use the self keyword in such places to refer to static members of the class to avoid the above error. While using self for referring to static data, we need to use the scope resolution operator. For example,
<?php
class PageUI
{
public static $menu;
public static function getMenu()
{
return self::$menu;
}
}
?>
In the above code sample, we have shown how the static variable $menu is referred PHP self-keyword.
self | this |
the self-keyword will not be preceded by any symbol; rather we can use it as it is. | But PHP this keyword should be preceded with a $ sign while referring class members. |
PHP scope resolution operator will be used with the self keyword. For example, self::<class-member> | -> symbol is used with $this variable as we have used with an object instance to access the property of that object. For example, $this-><class-member> |
It will be used for referring to a static member of a class. | this is used for accessing non-static members with -> operator. |
PHP self is referring to class members, not for any particular instance; rather, all of the class instances will use the same static member by the use of self. | But, $this will refer class members for a particular instance of the class. |
Let us see the above differences with a simple PHP program as an example to know, how the class members are referred to these two PHP keywords.
<?php
class Toys
{
public $toys_name;
public $toys_category;
public static $shop_name;
function Toys($name, $category)
{
$this->toys_name = $name;
$this->toys_category = $category;
}
public function getToyName()
{
return $this->toys_name;
}
public function getToyCategory()
{
return $this->toys_category;
}
public function getToyShop_nonStatic()
{
return self::getToyShop();
}
public static function getToyShop()
{
return self::$shop_name;
}
public static function setToyShop($shopname)
{
self::$shop_name = $shopname;
}
}
$objToys = new Toys("Battery Car", "Battery Toys");
$toys_name = $objToys->getToyName();
$toys_category = $objToys->getToyCategory();
echo "<br/>Toy: " . $toys_name . ", Category: " . $toys_category;
Toys::$shop_name = "Disney";
$shop_name = Toys::getToyShop();
echo "<br/>Shop Name: " . $shop_name;
Toys::setToyShop("ToyShop");
$shopname = Toys::getToyShop_nonStatic();
echo "<br/>Shop Name via non static function: " . $shopname;
?>
In the above program, we have three member variables for the Toys class. Two of those are non-static, expected to be initialized automatically while creating class instances. And, the class contains only one static variable commonly referred to for all instances of the class.
With constructors, the class variables are referred to by using $this, for initialization. Again, using $this variable the initialized members variables are referred to for class getters defined as a non-static member function, returning required values for a particular class instance.
On the other hand, static members are referred to as a self keyword to return the static value of $shop_name by the static member function getToyShop() of the Toys class.
Outside the class, first, we have created an instance with two arguments, for automatically calling the class constructor. With respect to this object or instance, the non-static member functions are invoked to get the value of $toys_name and $toys_category. Now, member functions use $this to refer to those values initialized on instantiation.
And then, the static members are accessed from outside the class by using the class name itself. But, within class scope, we have used self to access those members. In this program, we have retrieved static variable values by using non-static member function getToyShop_nonStatic(), also.
After executing the above PHP program, it will return the following output to the browser to be displayed.
Toy: Battery Car, Category: Battery Toys
Shop Name: Disney
Shop Name via non static function: ToyShop
Download PHP self Vs this Source Code
good article.
thanks you very much.
Thank you Porya.
It’s beautiful explanation.
Cleared my confusion as well.
Thank you.
Welcome Leslin.