The $this Keyword
The $this keyword is one of the most important concepts in Object Oriented Programming.
Every PHP developer must fully understand it because almost every framework relies heavily on it.
Examples:
$this->getServer();
$this->saveDefaultConfig();
$this->getConfig();
$this->getLogger();
$this->getScheduler();
PocketMine plugins use $this everywhere.
What is $this?
$this refers to:
The Current Object Instance
or simply:
THIS OBJECT
Example
class Player {
}
Object:
$player =
new Player();
Inside methods:
$this
refers to:
$player
Visualization
$player
↓
Player Object
Inside object:
$this
points back to:
Current Player Object
Why Do We Need $this?
Consider:
class Player {
public string $name;
}
Method:
public function setName(
string $name
) {
?
}
How does PHP know which object's name should be changed?
Player1?
Player2?
Player3?
This is why $this exists.
Example
class Player {
public string $name;
public function setName(
string $name
): void {
$this->name =
$name;
}
}
Usage
$p1 =
new Player();
$p2 =
new Player();
$p1->setName(
"Aayan"
);
$p2->setName(
"Steve"
);
Memory:
p1 → name = Aayan
p2 → name = Steve
Without $this
This:
$name = $name;
does absolutely nothing.
Correct:
$this->name = $name;
Visualization
$this->name
↑
Object Property
$name
↑
Method Parameter
Accessing Properties
Example:
class Player {
public int $money = 0;
public function addMoney(
int $amount
): void {
$this->money +=
$amount;
}
}
Accessing Methods
Methods can call other methods.
Example:
class Player {
public function hello() {
echo "Hello";
}
public function test() {
$this->hello();
}
}
Usage:
$player->test();
Output:
Hello
Accessing Multiple Methods
$this->save();
$this->load();
$this->update();
Real PocketMine Example
$this->saveDefaultConfig();
$this->getLogger();
$this->getConfig();
All methods belong to current plugin object.
Example Plugin
class Main
extends PluginBase {
protected function onEnable()
: void {
$this->saveDefaultConfig();
}
}
$this refers to:
Main Plugin Object
Accessing Current Server
$this->getServer();
Equivalent to:
CurrentPlugin
↓
Server Object
Method Chaining
Methods may return:
$this
Example:
class Player {
public function setName(
string $name
): self {
$this->name =
$name;
return $this;
}
}
Usage
$player
->setName("Aayan")
->setMoney(5000)
->setRank("Owner");
This is called:
Method Chaining
Fluent Interface
Used heavily in Laravel.
Example:
$query
->where()
->orderBy()
->limit();
Internally:
return $this;
$this and Memory
Example:
$p1 =
new Player();
$p2 =
new Player();
Each object has its own $this.
Memory:
p1 → Object1
p2 → Object2
Inside Object1:
$this = Object1
Inside Object2:
$this = Object2
Example
class Player {
public string $name;
public function show() {
var_dump($this);
}
}
Output:
object(Player)
$this Inside Closures
Example:
class Test {
public function hello() {
$fn =
function () {
var_dump(
$this
);
};
$fn();
}
}
Modern PHP automatically binds $this.
Static Context
This is extremely important.
Wrong
class Test {
public static function hello() {
$this->test();
}
}
Error:
Using $this when not in object context
Why?
Static methods belong to:
Class
not:
Object
No object exists.
Therefore:
$this
does not exist.
Correct
self::test();
Example
class Math {
public static function add() {
}
public static function test() {
self::add();
}
}
self vs $this
$this
Current Object.
self
Current Class.
Example:
$this->money
Property of object.
Example:
self::$count
Static property of class.
Visualization
Object
│
├── $this
│
Class
│
└── self
$this and Constructors
Constructors commonly use:
$this->name =
$name;
Example:
class Player {
private string $name;
public function __construct(
string $name
) {
$this->name =
$name;
}
}
PocketMine Examples
Logger
$this->getLogger()
->info(
"Enabled"
);
Scheduler
$this->getScheduler()
->scheduleTask(
...
);
Config
$this->getConfig()
->get(
"motd"
);
Server
$this->getServer()
->broadcastMessage(
"Restarting"
);
Object Composition
$this->database
->connect();
$this accesses:
Database Object
which then executes:
connect()
Common Beginner Mistakes
Forgetting $this
Wrong:
name = "Aayan";
Correct:
$this->name =
"Aayan";
Using $this Inside Static Methods
Wrong:
public static function test() {
$this->hello();
}
Accessing Undefined Properties
Wrong:
$this->coins
when property doesn't exist.
Mixing Variables
Wrong:
$name = $name;
Correct:
$this->name =
$name;
Best Practices
✅ Use $this for object properties.
✅ Keep object state inside properties.
✅ Use method chaining carefully.
✅ Understand difference between $this and self.
Exercises
Exercise 1
Create:
class Car {
private string $brand;
public function setBrand(
string $brand
) {
$this->brand =
$brand;
}
}
Exercise 2
Create:
Player
Methods:
setName()
setMoney()
setRank()
using $this.
Exercise 3
Implement method chaining.
Mini Project
Create:
PlayerBuilder
Usage:
$player
->setName("Aayan")
->setRank("Owner")
->setMoney(10000);
Quiz
What does $this refer to?
Answer
Current object instance.
Can $this be used in static methods?
Answer
No.
Which keyword refers to current class?
Answer
self
Which keyword refers to current object?
Answer
$this
Real PocketMine Usage
$this->getServer()
$this->getLogger()
$this->getScheduler()
$this->saveDefaultConfig()
$this->reloadConfig()
Understanding $this is essential for plugin development.
References
https://www.php.net/manual/en/language.oop5.basic.php
Next Chapter
➡ Access Modifiers
➡ Encapsulation
➡ Inheritance