PHP self Vs this

In PHP, the self and this keywords are used to refer class members within the scope of a class. The class members can be of either variables or functions. These PHP keywords differs with the static behavior of the class members.

PHP this keyword refers 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 value with which the instance of the class is created.

php_self_vs_this

If we use this keyword without object context for referring 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 self keyword in such places to refer static members of the class to avoid the above error. While using self for referring static data, we need to use scope resolution operator. For example,

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 by PHP self keyword.

Difference between PHP self and this

self this
self keyword will not be preceded with any symbol; rather we can use as it is. But PHP this keyword should be preceded with $ sign, while referring class members.
PHP scope resolution operator will be used with 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 static member of a class. this is used for accessing non-static member with -> operator.
PHP self is referring class members not for any particular instance; rather, all of the class instances will use same static member by the use of self. But, $this will refer class members for a particular instance of the class.

Example: PHP self Vs this

Let us see the above differences with a simple PHP program as an example to know, how the class members are referred by 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 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 for all instances of the class.

With constructors, the class variables are referred by using $this, for initialization. Again, using $this variable the initialized members variables are referred 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 by using 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 argument, for automatically calling the class constructor. With respect to this object or instance, the non-static member functions are invoked to get value of $toys_name and $toys_category. Now, member functions use $this to refer those values initialized on instantiation.

And then, the static members are accessed from outside the class by using the class name itself. But, with in 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

This PHP code tutorial was published on August 28, 2013.

↑ Back to Top