PHP Methods
Methods are functions that belong to a class.
They define what an object can do.
Examples:
Player
│
├── sendMessage()
├── teleport()
├── kick()
└── addMoney()
Properties describe:
WHAT an object IS
Methods describe:
WHAT an object DOES
Real Life Example
Car:
Properties:
brand
speed
color
fuel
Methods:
start()
stop()
accelerate()
brake()
Declaring Methods
class Player {
public function hello() {
echo "Hello";
}
}
Calling Methods
$player =
new Player();
$player->hello();
Output:
Hello
Visualization
Player Object
│
├── name
├── money
│
├── sendMessage()
├── addMoney()
└── teleport()
Multiple Methods
class Player {
public function join() {
echo "Joined";
}
public function leave() {
echo "Left";
}
}
Methods With Parameters
Methods can receive data.
class Player {
public function sendMessage(
string $message
) {
echo $message;
}
}
Usage:
$player->sendMessage(
"Hello World"
);
Multiple Parameters
public function teleport(
float $x,
float $y,
float $z
) {
}
Methods Returning Values
public function getMoney(): int {
return 5000;
}
Usage:
echo
$player->getMoney();
Return Types
Methods should always declare return types.
Available:
int
string
float
bool
array
object
void
mixed
never
Void Methods
public function kick(): void {
}
Means:
Returns nothing.
Returning Objects
public function getServer()
: Server {
}
Returning Nullable Objects
public function getTarget()
: ?Player {
}
Means:
Player OR null
Method Visibility
Methods support:
public
protected
private
Public Methods
Accessible everywhere.
public function hello() {
}
Protected Methods
Only current class and child classes.
protected function save() {
}
Private Methods
Only current class.
private function encrypt() {
}
Example
class Bank {
private function verify() {
}
}
Nobody outside can call:
$bank->verify();
The $this Keyword
Inside methods:
$this
refers to current object.
Example:
class Player {
public int $money = 0;
public function addMoney(
int $amount
): void {
$this->money +=
$amount;
}
}
Example
$player =
new Player();
$player->addMoney(
1000
);
echo
$player->money;
Output:
1000
Why $this Exists
Imagine:
$p1
$p2
$p3
Each object has its own money.
$this
automatically refers to correct object.
Method Chaining
Methods can return:
$this
Example:
public function setName(
string $name
): self {
$this->name =
$name;
return $this;
}
Usage:
$player
->setName("Aayan")
->setRank("Owner")
->setMoney(5000);
Self Return Type
public function test()
: self {
return $this;
}
Means:
Returns current class.
Fluent Interface
Popular in Laravel.
Example:
$query
->where()
->orderBy()
->limit();
Method Overloading
PHP does not support true method overloading.
Invalid:
public function test() {}
public function test(
string $name
) {}
Error.
Alternative
Use:
mixed
or
...$args
Example:
public function test(
mixed ...$args
) {
}
Named Arguments
PHP 8+
createPlayer(
name: "Aayan",
money: 5000
);
Recursive Methods
Methods may call themselves.
Example:
public function factorial(
int $n
): int {
if ($n <= 1) {
return 1;
}
return
$n *
$this->factorial(
$n - 1
);
}
Static Methods
Belong to class itself.
public static function create() {
}
Usage:
Player::create();
Example
class Math {
public static function add(
int $a,
int $b
): int {
return $a + $b;
}
}
Usage:
Math::add(
5,
10
);
Abstract Methods
Can be declared without implementation.
abstract public function save();
Covered later.
Final Methods
Prevent overriding.
final public function getId() {
}
Covered later.
Real PocketMine Examples
Sending Message
$player->sendMessage(
"Hello"
);
Teleporting
$player->teleport(
$position
);
Getting Name
$player->getName();
Server Broadcast
$this->getServer()
->broadcastMessage(
"Restarting"
);
Event Example
public function onJoin(
PlayerJoinEvent $event
): void {
$player =
$event->getPlayer();
$player->sendMessage(
"Welcome!"
);
}
Method Call Stack
Example:
A()
↓
B()
↓
C()
Each method creates a stack frame.
Memory Representation
Object
│
├── Properties
└── Methods
Methods are stored once.
Properties are unique per object.
Common Beginner Mistakes
Forgetting Parentheses
Wrong:
$player->sendMessage;
Correct:
$player->sendMessage();
Forgetting Return
Wrong:
public function getMoney()
: int {
}
Must return integer.
Accessing Undefined Methods
Wrong:
$player->abc();
Using Static Incorrectly
Wrong:
$this->create();
if method is static.
Correct:
self::create();
Best Practices
✅ Use return types.
✅ Keep methods small.
✅ One responsibility per method.
✅ Avoid huge methods.
✅ Prefer meaningful names.
Naming Examples
Good:
sendMessage()
getMoney()
setRank()
teleport()
Bad:
abc()
test()
method1()
Exercises
Exercise 1
Create:
class Car {
public function start() {
}
}
Exercise 2
Create:
Player
Methods:
sendMessage()
kick()
ban()
Exercise 3
Create:
Economy
Methods:
addMoney()
removeMoney()
hasMoney()
Mini Project
Create:
class Player {
private string $name;
private int $money;
public function addMoney() {}
public function removeMoney() {}
public function sendMessage() {}
}
Quiz
What are methods?
Answer
Functions belonging to classes.
Which keyword refers to current object?
Answer
$this
Which operator calls methods?
Answer
->
Which keyword creates class methods?
Answer
function
Which keyword creates static methods?
Answer
static
Real PocketMine Architecture
Player
│
├── sendMessage()
├── teleport()
├── kick()
├── setGamemode()
└── getInventory()
Everything in PocketMine revolves around methods.
References
https://www.php.net/manual/en/language.oop5.php
Next Chapter
➡ Constructors and Destructors ➡ Access Modifiers ➡ Encapsulation