this in PHP class
this in PHP class: In the previous tutorial, we have seen how to create a class, objects, properties, and methods. We have learns how a class can be used to to make an object which is a combination of properties and methods.
We have created two objects $iphone and $mobile of a class Mobile. And both these objects having different properties values. We will explore this class and objects here more.
The last example was:
class Mobile { public $companyName; public $color = "Red"; public $hasCamera= true; public function calling() { return "Calling from a mobile."; } }
Use of this in PHP class
In the previous chapter we have seen how you can access class properties outside a class using an object with an arrow and a property name, like
echo $iphone->companyName;
Now if you want to access the properties inside a method of a class, to perform some functionalities then you will be required to use a keyword $this.
Look into the code below.
class Mobile { public $companyName; public $color = "Red"; public $hasCamera= true; public function calling() { return "Calling from a ". $this->companyName." mobile, with ".$this->color." color."; } }
As you see in a method “calling()” we have used two properties “companyName” and “color” inside a string.
But here we have used “$this” with a property name. $this->comanyName and $this->color will show the current value of comanyName and color respectively when a class is in use.
$this keyword represents the current scope of an object. It means $this will show the properties and methods related to the currently working object of a class. $this provides the reference to the current calling object of a class.
Let’s see the above concept using an example.
Let’s create two objects of a Mobile class, assign properties and access the method calling().
Create objects of a class Mobile:
$iphone = new Mobile(); $galaxy = new Mobile();
Now set the values of properties for both the objects.
For $iphone
$iphone->companyName = "Apple"; $iphone->color= "Black";
For $galaxy
$galaxy ->companyName = "Samsung"; $galaxy ->color= "White";
Now, let’s call the method calling() for each object and let’s see the output.
echo $iphone->calling(); echo "<br>"; echo $galaxy->calling();
The output of the above code will be:
Calling from a Apple mobile, with Black color. Calling from a Samsung mobile, with White color.
Whenever we create an object and set properties then the values of properties will be set as per the current object.
Like when we created $iphone and set properties then after calling a method $this uses the properties as per the $iphone object. And same happens for $galaxy object.
So the “$this” keyword works as per its name, it says – “this is the current object working on a class, its properties, and its methods. So use the properties and methods of this object only”.
$this tells that the current values of properties and the methods only belong to the current working object.
Let’s see the complete program below
class Mobile { public $companyName; public $color = "Red"; public $hasCamera= true; public function calling() { return "Calling from ". $this->companyName." mobile, with ".$this->color." color."; } } $iphone = new Mobile(); $galaxy = new Mobile(); $iphone->companyName = 'Apple'; $iphone->color= "Black"; $galaxy->companyName = 'Samsung'; $galaxy ->color= "White"; echo $iphone->calling(); echo "<br>"; echo $galaxy->calling();
Setting properties inside a method
Using this in PHP class, you can also set the properties inside a method by passing the value as a parameter to the method.
Let’s update a method calling() to catch values for a company name and a color. And set the company name and color properties inside a method.
public function calling($company, $color) { $this->compayName = $company; $this->color= $color; return "Calling from a ". $this->companyName." mobile, with ".$this->color." color."; }
Now call the method by using objects and passing values of company and color.
echo $iphone->calling('Apple','Black'); echo $galaxy->calling('Samsung','White');
The output of the above statements will be:
Calling from Apple mobile, with Black color. Calling from Samsung mobile, with White color.
So using this in PHP class variable you can set properties and use the method as per the currently calling object of a class.
In the next tutorial, we will see the concept constructors and destructors PHP, OOPs using PHP magic methods.