php class this – Object Oriented Programming – PHP $this class

php class this – In PHP i can get the class instance inside the class methods to get variables using the $this variable.

php class this & PHP this and self Keyword

PHP $this class – Hence this keyword is quite useful in PHP. In PHP, the self and this keyword are used to refer class members within the scope of a class. $this is a reserved keyword in PHP that refers to the calling object.

Fatal error: Using $this when not in object context in … on Line

class PageUI {
public static $menu;
...
public static function getMenu() {
return self::$menu;
}

Access current class properties inside current class method.

Difference between PHP self and this

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

Example: PHP self Vs this

<?php
class Products{
public $Products_name;
public $Products_Type;
public static $Store_name;

function Products($name,$Type) {
$this->Products_name = $name;
$this->Products_Type = $Type;
}

public function getProductName() { 
return $this->Products_name; 
}
public function getProductType() { 
return $this->Products_Type; 
}	
public function getProductStore_nonStatic() { 
return self::getProductStore(); 
}
public static function getProductStore() { 
return self::$Store_name;
}
public static function setProductStore($Storename) { 
self::$Store_name=$Storename; 
}

}

$objProducts = new Products("Game Car","Game Products");
$Products_name = $objProducts->getProductName();
$Products_Type = $objProducts->getProductType();
echo "<br/>Product: " . $Products_name . ", Type: " . $Products_Type;

Products::$Store_name = "Infinityknow";
$Store_name = Products::getProductStore();
echo "<br/>Store Name: " . $Store_name;

Products::setProductStore("ProductStore");
$Storename = Products::getProductStore_nonStatic();
echo "<br/>Store Name via non static function: " . $Storename;
?>

In the above Example, i have 3 User-defined variables for Products class. 2 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.

Read Also:  Pagination with jQuery Ajax PHP and MySQL

When to use self over $this in PHP ?

With constructors, the class variables are referred by using $this, for initialization. Again, using $this variable the initialized User-defineds variables are referred for class getters defined as a non-static User-defined function, returning required values for a specific class instance.

On the other hand, static User-defineds are referred by using a self keyword to return the static value of $Store_name by the static User-defined function getProductStore() of the Products class.

Outside the class, first of all, i have created an instance with 2 main arguments, for automatically calling the class constructor. With respect to this object or instance, the non-static User-defined functions are invoked to get the value of $Products_name and $Products_Type. Now, User-defined functions use $this to refer those values initialized on instantiation.

And then, the static User-defineds are accessed from outside the class by using the class name itself. But, within class scope, i have used self to access those User-defineds. In this Example, i have retrieved static variable values by using non-static User-defined function getProductStore_nonStatic(), also.

And then executing the above PHP Example, it will return the bellow results to the browser to be showed.

Read Also:  Export Data to Excel in Laravel Example

Product: Game Car, Type: Game Products
Store Name: Infinityknow
Store Name via non static function: ProductStore

Access current class properties inside current class method

<?php

class example
{

	var $new=1000;
	var $land=500;

	function add( )
	{   
		$add=$this->new + $this->land;
		echo "addition=".$add."<br/>";	
	}

	function sub( )
	{

		$sub=$this->new - $this->land;
		echo "subtraction=".$sub;	

	}

}

$obj= new example();
$obj->add();
$obj->sub();
?>

I hope you get an idea about php class this.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.