Skip to main content

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

ModifierSame ClassChild ClassOutside
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

├── public sendMessage()
├── public teleport()
└── public kick()

Most internals remain private.


Common Beginner Mistakes


Making Everything Public

Bad:

public int $money;
public int $health;
public int $rank;

Making Everything Private

Bad:

private function sendMessage()

Nobody can use it.


Using Protected Incorrectly

Use protected only when inheritance is required.


Exposing Sensitive Data

Bad:

public string $password;

Best Practices

✅ Properties should usually be private.

✅ Use public methods for access.

✅ Use protected for inheritance.

✅ Never expose sensitive information.


Recommended Rule

Private by default.

Only make things public when necessary.


Exercises


Exercise 1

Create:

class Bank {

private float $balance;

}

Exercise 2

Create getter:

getBalance()

Exercise 3

Create setter with validation.


Mini Project

Create:

Player

Properties:

name
money
rank
health

Use proper access modifiers.


Quiz

Which modifier is accessible everywhere?

Answer
public

Which modifier is only accessible in same class?

Answer
private

Which modifier supports inheritance?

Answer
protected

Which modifier should be used by default?

Answer
private

Summary

ModifierSame ClassChildOutside
public
protected
private

Real PocketMine Example

class Main extends PluginBase {

private Database $database;

protected function onEnable()
: void {

$this->database =
new Database();

}

public function getDatabase()
: Database {

return $this->database;

}

}

References

https://www.php.net/manual/en/language.oop5.visibility.php


Next Chapter

➡ Encapsulation

➡ Inheritance

➡ Polymorphism