$this refers to the current object.
$this is a pseudo-variable (also a reserved keyword) which is only available inside methods. And, it refers to the object of the current method.
Let's create a House class with one property (color). Then, create an object from it.
<?php class House { public $color = 'black'; } $house = new House();
Where to use $this?
How can we change the value of $color? Take some time and think.
There are two ways.
- Outside the class. We can directly change the value of a property from outside of the class.
$house -> color = 'white';
- Inside the class. We can define a method in the class and call it to change the value of its own property.
public function changeColor() { $this -> color = 'white'; }
In the second way, we use $this variable to access the current object. Here, the current object is the object saved in $house.
More Examples
Let's play with the $this pseudo-variable.
Here we will create two houses and assign names and colors to them. Then, we will echo it out.
- Create the House class
<?php class House { public $name; public $color; public function setData($name, $color) { $this -> name = $name; $this -> color = $color; } public function echoData() { echo "The color of the {$this -> name} is {$this -> color}"; } }
- Create two objects from it
$blackHouse = new House(); $whiteHouse = new House();
- Call the setData method with two arguments
$blackHouse -> setData("John's House", "black"); $whiteHouse -> setData("Pearl's House", "white");
- Call the echoData to echo out a descriptive phrase
$blackHouse -> echoData(); echo '<br>'; $whiteHouse -> echoData();
Here's the full code.
PHP OOP $this Keyword Example
<?php class House { public $name; public $color; public function setData($name, $color) { $this -> name = $name; $this -> color = $color; } public function echoData() { echo "The color of the {$this -> name} is {$this -> color}"; } } $blackHouse = new House(); $whiteHouse = new House(); // this is a small house, not america's one ;) $blackHouse -> setData("John's House", "black"); $whiteHouse -> setData("Pearl's House", "white"); $blackHouse -> echoData(); echo '<br>'; $whiteHouse -> echoData();Run Example ››
Conclusion
We could access the current object with $this keyword. But, we needed to run two steps of code to create a object ($house = new House()) and set its data ($house -> setData(...)). What if we could set the data when we create the object? We can run a constructor function when the object is created. You can learn more in the next chapter.
Next ❯
PHP OOP Constructors and Destructors
Summary: in this tutorial, you will learn about PHP $this keyword and how to use $this inside a class to reference the current object.
In PHP, $this keyword references the current object of the class. The $this keyword allows you to access the properties and methods of the current object within the class using the object operator (->):
$this->property $this->method()
Code language: PHP (php)The $this keyword is only available within a class. It doesn’t exist outside of the class. If you attempt to use the $this outside of a class, you’ll get an error.
When you access an object property using the $this keyword, you use the $ with the this keyword only. And you don’t use the $ with the property name. For example:
$this->balance
Code language: PHP (php)The following shows the BankAccount class:
<?php class BankAccount { public $accountNumber; public $balance; public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } } public function withdraw($amount) { if ($amount <= $this->balance) { $this->balance -= $amount; return true; } return false; } }
Code language: HTML, XML (xml)In this example, we access the balance property via the $this keyword inside the deposit() and withdraw() methods.
Chaining methods
First, create a new BankAccount object:
// create a new account object $account = new BankAccount(); $account->accountNumber = 1; $account->balance = 100;
Code language: PHP (php)Second, call the deposit() method three times to deposit different amounts of money:
$account->deposit(100); $account->deposit(200); $account->deposit(300);
Code language: PHP (php)This code is quite verbose. It would be more concise and expressive if you can write these statements using a single statement like this:
$account->deposit(100) ->deposit(200) ->deposit(300);
Code language: PHP (php)This technique is called method chaining. To form the method chaining, the deposit() method needs to return a BankAccount object, which is the $this inside the BankAccount class like this:
public function deposit($amount) { if ($amount > 0) { $this->balance += $amount; } return $this; }
Code language: PHP (php)The deposit() returns the $this which is the current object of the BankAccount class. Therefore, you can call any public method of the BankAccount class.
The following example calls the deposit() method first and then the withdraw() method in a single statement:
$account->deposit(100) ->withdraw(150);
Code language: PHP (php)It’s equivalent to the following:
$account->deposit(100); $account->withdraw(150);
Code language: PHP (php)Summary
- PHP $this keyword references the current object of the class. It’s only available within the class.
- Do use the method chaining by returning $this from a method to make the code more concise.
Did you find this tutorial useful?