Access Modifiers
Access Modifiers determine:
Who can access properties and methods.
They are one of the core concepts of Object Oriented Programming.
Without access modifiers, your code can become insecure and difficult to maintain.
Why Access Modifiers Exist
Imagine this:
$player->money = 999999999;
$player->rank = "Owner";
$player->health = -500;
Anyone could modify anything.
This is dangerous.
Access modifiers help protect object data.
PHP Provides Three Access Modifiers
public
protected
private
Visualization
Class
│
├── public
├── protected
└── private
1. Public
Public members can be accessed from anywhere.
Example
class Player {
public string $name;
}
Usage:
$player =
new Player();
$player->name =
"Aayan";
Valid.
Visualization
Anywhere
↓
public
Public Methods
class Player {
public function hello() {
echo "Hello";
}
}
Usage:
$player->hello();
Real PocketMine Example
$player->sendMessage();
$player->teleport();
$player->kick();
All of these methods are public.
Problems With Public
Bad:
class Player {
public int $money = 0;
}
Now anyone can do:
$player->money =
999999999;
This breaks validation.
2. Private
Private members can ONLY be accessed inside the same class.
Example
class Player {
private int $money = 0;
}
Invalid:
$player->money = 500;
Error:
Cannot access private property.
Valid
class Player {
private int $money = 0;
public function addMoney(
int $amount
) {
$this->money +=
$amount;
}
}
Usage:
$player->addMoney(
500
);
Why Private is Important
Because it allows validation.
Example:
public function addMoney(
int $amount
) {
if ($amount < 0) {
return;
}
$this->money +=
$amount;
}
Real Example
Bank Account:
private float $balance;
Nobody should modify balance directly.
3. Protected
Protected members can be accessed:
✅ Inside current class
✅ Inside child classes
❌ Outside class
Example
class Human {
protected int $health = 20;
}
Child:
class Player
extends Human {
public function heal() {
$this->health += 5;
}
}
Valid.
Outside:
$player->health;
Invalid.
Visualization
Parent
↓
Child
protected accessible
Comparison Table
| Modifier | Same Class | Child Class | Outside |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |
Real Example
class Player {
public string $name;
protected int $health;
private int $money;
}
Access:
$player->name;
Valid.
Access:
$player->health;
Invalid.
Access:
$player->money;
Invalid.
Inheritance Example
class Human {
protected int $health = 20;
}
Child:
class Player
extends Human {
public function show() {
echo
$this->health;
}
}
Works.
Private Example
class Human {
private int $health = 20;
}
Child:
echo
$this->health;
Error.
Why?
Because private belongs ONLY to Human.
Child classes cannot see it.
Visualization
Human
│
├── private
│
Player
Player cannot access Human's private members.
Access Modifiers For Methods
Methods also support visibility.
Public Method
public function sendMessage() {
}
Protected Method
protected function save() {
}
Private Method
private function encrypt() {
}
Example
class Database {
private function connect() {
}
}
Outside:
$db->connect();
Error.
Real PocketMine Example
Plugin methods:
public function onEnable()
must be public or protected.
Internal methods:
private function loadData()
can remain private.
Encapsulation Using Access Modifiers
Usually:
private properties
with:
public methods
Example:
private int $money;
public function getMoney()
This is called:
Encapsulation
Getters and Setters
Getter
public function getMoney()
: int {
return $this->money;
}
Setter
public function setMoney(
int $money
): void {
$this->money =
$money;
}
Validation Example
public function setHealth(
int $health
): void {
if (
$health < 0
) {
return;
}
$this->health =
$health;
}
Best Practice Example
class Player {
private int $money = 0;
public function addMoney(
int $amount
): void {
if (
$amount <= 0
) {
return;
}
$this->money +=
$amount;
}
}
Real PocketMine Architecture
Simplified:
Player
│
├── private NetworkSession
├── private Inventory
├── private Effects