Introduction to Object Oriented Programming
Object Oriented Programming (OOP) is one of the most important concepts in modern software development.
Almost every large application today uses OOP.
Examples:
- PocketMine-MP
- Laravel
- Symfony
- WordPress
- Discord Bots
- Android Apps
- Games
- APIs
- Enterprise Software
Why OOP Exists
Imagine building a server with:
- Players
- Worlds
- Commands
- Scoreboards
- Economy
- Friends
- Parties
Without OOP:
$player1Money = 5000;
$player2Money = 10000;
$player3Money = 15000;
After 500 players:
$player527Money
$player928Money
Impossible to manage.
Real Life Example
Think about a Car.
A car has:
Properties:
Color
Brand
Speed
Fuel
Actions:
Start()
Stop()
Accelerate()
Brake()
This is exactly how OOP works.
OOP Terminology
Class
Blueprint.
Example:
Player Blueprint
Object
Actual instance created from blueprint.
Aayan
Steve
Alex
Property
Variable inside class.
$name
$money
$rank
Method
Function inside class.
sendMessage()
kick()
ban()
Visualization
Player Class
│
├── name
├── money
├── rank
│
├── sendMessage()
├── addMoney()
└── kick()
Objects:
Player1
Player2
Player3
Memory Representation
Class Definition
↓
new Player()
↓
Object Created
↓
Stored In Memory
Creating a Class
class Player {
}
Creating Object
$player =
new Player();
Creating Multiple Objects
$p1 = new Player();
$p2 = new Player();
$p3 = new Player();
Each object has its own memory.
Why OOP is Powerful
Code Reuse
Without OOP:
sendMessagePlayer1();
sendMessagePlayer2();
sendMessagePlayer3();
With OOP:
$player->sendMessage();
Better Organization
Projects become easier to understand.
Easier Teamwork
Multiple developers can work simultaneously.
Scalability
Large projects become possible.
Four Pillars of OOP
1. Encapsulation
Protect data.
Example:
private int $money;
2. Inheritance
Reuse existing classes.
Entity
↓
Human
↓
Player
3. Polymorphism
Same method can behave differently.
4. Abstraction
Hide internal implementation.
Example:
$database->connect();
You don't need to know how it works internally.
Procedural vs OOP
Procedural
$name = "Aayan";
function kick() {
}
OOP
$player->kick();
Example
Procedural:
function addMoney(
&$money,
int $amount
) {
$money += $amount;
}
OOP:
$player->addMoney(
1000
);
Cleaner and easier.
PocketMine Example
This:
$player->sendMessage(
"Hello"
);
Internally:
Player Object
↓
Create Packet
↓
Serialize
↓
Send Through RakNet
This is abstraction.
Object Relationships
One Player belongs to one World
Player
↓
World
One Server contains many Players
Server
↓
Players[]
One Plugin contains many Commands
Plugin
↓
Commands[]
OOP Architecture Example
Server
│
├── Players
├── Worlds
├── Commands
├ ── Scheduler
├── Network
└── Plugins
Everything is objects.
Enterprise Example
Controller
↓
Service
↓
Repository
↓
Database
Entire architecture is OOP.
Advantages
✅ Reusability
✅ Cleaner Code
✅ Easier Maintenance
✅ Better Team Development
✅ Better Architecture
✅ Scalable Projects
Disadvantages
❌ Slightly more difficult initially.
❌ More files.
❌ More abstraction.
Where OOP is Used
- PHP Frameworks
- PocketMine-MP
- DragonFly
- Java
- C#
- Unreal Engine
- Android
- APIs
- Discord Bots
- Enterprise Applications
Common Beginner Mistakes
Thinking Classes Store Data
Classes are blueprints.
Objects store data.
Treating Objects Like Arrays
Wrong:
$player["money"];
Correct:
$player->getMoney();
Everything Public
Wrong:
public int $money;
Prefer:
private int $money;
Huge Classes
Bad:
PlayerManager
5000 lines
Split responsibilities.
Exercises
Exercise 1
Create blueprint:
Car
Properties:
brand
color
speed
Exercise 2
Create blueprint:
Server
Methods:
start()
stop()
broadcast()
Exercise 3
Think of real life objects and convert them into classes.
Mini Project
Design architecture:
Player
Party
Guild
Economy
How do these interact?
Quiz
What is a Class?
Answer
Blueprint used to create objects.
What is an Object?
Answer
Instance created from class.
Name four pillars of OOP.
Answer
Encapsulation
Inheritance
Polymorphism
Abstraction
Real PocketMine Hierarchy
PluginBase
↓
Main
Entity
↓
Human
↓
Player
References
PHP OOP Documentation:
https://www.php.net/manual/en/language.oop5.php
Next Chapter
➡ Classes and Objects