How And Why Should We Use ‘this’ Keyword in PHP?

In order to access attributes in a class itself we need to use ‘this’ keyword. This is the object in use right now in class while we are working. We can try to price $this at any time to show its condition. I am going to do this. So imagine this product class in our shop scenario. 

class Product {

public $name;
public $priceWithoutTaxes;
public $tax;
public $priceWithTax;

public function setName( $name ) {
$this->name = $name;
}

public function setPriceWithoutTax( $priceWithoutTaxes ) {
$this->priceWithoutTaxes = $priceWithoutTaxes;
}

public function setTax( $tax ) {
$this->tax = $tax;
}

public function setPriceWithTax() {
$this->priceWithTax = $this->priceWithoutTaxes + $this->tax;
}
}

Now lets write the code for it and actually use this class.

$product = new Product();
$product->setName( 'Purel' );
$product->setPriceWithoutTax( 45 );
$product->setTax( 5 );
$product->setPriceWithTax();

Lets say we will print $this in setPriceWithTax function. So code will change just a little.

public function setPriceWithTax() {
		print_r($this);
		$this->priceWithTax = $this->priceWithoutTaxes + $this->tax;
	}

And you will observe this output

.