Abstraction
Chapter Overview
In this chapter you will learn:
- What abstraction means
- Why abstraction exists
- Real life examples
- How abstraction works internally
- Difference between abstraction and encapsulation
- Abstract thinking in software engineering
- Abstract classes
- Interfaces
- Framework architecture
- PocketMine examples
- Enterprise examples
- Design principles using abstraction
- Best practices
- Projects and exercises
What is Abstraction?
Abstraction means:
Hiding unnecessary implementation details
and exposing only essential functionality.
Or simply:
Focus on WHAT something does,
not HOW it does it.
Real Life Example
When driving a car:
You use:
- Steering Wheel
- Brake
- Accelerator
- Gear
You do NOT need to know:
- Engine combustion
- Fuel injection timings
- ECU calculations
- Gear synchronization
- Piston movement
- Sensor communication
All of that complexity is hidden.
This is abstraction.
Another Example
Think about a television.
You press:
Power Button
You do not care:
Voltage calculations
Motherboard signals
Display drivers
Hardware initialization
You simply care:
TV Turns On
This is abstraction.
Programming Example
Without abstraction:
$socket = socket_create(...);
$packet = PacketEncoder::encode(...);
$compressed =
Compression::compress(
$packet
);
socket_write(
$socket,
$compressed
);
Very complicated.
With abstraction:
$player->sendMessage(
"Hello"
);
Much simpler.
Definition
Abstraction helps developers:
✅ Reduce complexity
✅ Write cleaner code
✅ Reuse code
✅ Understand systems easier
✅ Hide internals
Why Abstraction Exists
Imagine if every developer had to understand:
- CPU Instructions
- Networking Packets
- Operating System APIs
- Memory Management
before sending a chat message.
Software development would become impossible.
Abstraction allows developers to build:
Complex Systems
using
Simple Building Blocks
Layers of Abstraction
Modern software consists of multiple layers.
Example:
Application
↓
Framework
↓
Libraries
↓
Operating System
↓
Kernel
↓
Hardware
Every layer hides complexity.
Example
When using:
echo "Hello";
PHP internally performs:
Output Buffer
Encoding
Stream Handling
Operating System Calls
Terminal Drivers
You never see these details.
This is abstraction.
Visualization
You
↓
sendMessage()
↓
Packet Creation
↓
Compression
↓
Encryption
↓
Socket Sending
↓
Client Receives Message
Only one line of code is visible.
Real PocketMine Example
You write:
$player->teleport(
$position
);
Internally PocketMine does:
Chunk Validation
Event Calling
Movement Validation
Position Update
Network Packet Sending
Client Synchronization
You never see this.
This is abstraction.
Example
$player->kick(
"Cheating"
);
Internally:
Disconnect Packet
Session Closing
Player Saving
Event Dispatching
Plugin Handling
All hidden.
Database Example
You write:
$user = User::find(1);
Internally:
Connection Pool
SQL Generation
Prepared Statements
Result Parsing
Object Creation
Caching
All hidden.
The Main Goal
Abstraction answers:
What can I do?
Instead of:
How is it implemented?
High Level vs Low Level
High Level
$mail->send();
Simple.
Low Level
Socket
SMTP Packets
Authentication
Headers
Encoding
Compression
Complex.
Example
Without abstraction:
Packet::create();
Packet::compress();
Packet::send();
With abstraction:
$player->sendMessage();
Abstraction in Frameworks
Frameworks exist because of abstraction.
Examples:
- Laravel
- Symfony
- PocketMine
- Nukkit
- DragonFly
All hide complexity.
Example
Laravel:
User::create([
"name" => "Aayan"
]);
Internally:
SQL Generation
Database Connection
Validation
Mass Assignment Protection
Events
Hidden.
Abstraction Everywhere
Examples:
Browser APIs
Operating Systems
Databases
Game Engines
Networking
Cloud Services
Everything uses abstraction.
Difference Between Abstraction and Encapsulation
Many beginners confuse these.
Abstraction
Hides complexity.
Encapsulation
Hides data.
Example
Abstraction
$car->start();
You don't know HOW.
Encapsulation
private Engine $engine;
You cannot access it directly.
Comparison Table
| Feature | Abstraction | Encapsulation |
|---|---|---|
| Hides Complexity | ✅ | ❌ |
| Hides Data | ❌ | ✅ |
| Focus | What | Protection |
| Uses | Interfaces, Abstract Classes | Access Modifiers |
Abstraction in OOP
PHP mainly provides abstraction through:
1. Abstract Classes
2. Interfaces
Abstract Thinking
Consider:
Animal
What is an animal?
Animals:
- Eat
- Sleep
- Move
This becomes an abstraction.
Dog:
Dog IS AN Animal
Cat:
Cat IS AN Animal
Bird:
Bird IS AN Animal
Abstract Class Example
abstract class Animal {
abstract public function sound();
}
Children:
class Dog
extends Animal {
public function sound() {
echo "Bark";
}
}
class Cat
extends Animal {
public function sound() {
echo "Meow";
}
}
Why?
Because every animal has:
sound()
But implementation differs.
Interface Example
interface Logger {
public function log(
string $message
);
}
Implementations:
File Logger
Discord Logger
Database Logger
Real World Architecture
Application
↓
Service
↓
Repository
↓
Database
Every layer abstracts another layer.
PocketMine Architecture Example
Plugin developer writes:
$this->getServer()
Internally:
PluginBase
↓
Server Singleton
↓
WorldManager
↓
Network
↓
Scheduler
Hidden complexity.
Another Example
You use:
$world->getPlayers();
Internally:
Chunk Manager
Entity Manager
Session Manager
Collections
All hidden.
Benefits of Abstraction
1. Simpler Code
Instead of:
200 lines
You write:
$player->kick();
2. Better Readability
Example:
$order->pay();
Much easier.
3. Easier Maintenance
Internal implementation can change.
Outside code remains same.
Example
Today:
File Database
Tomorrow:
MySQL
Outside code:
$user->save();
never changes.
4. Scalability
Large systems depend heavily on abstraction.
Without it:
Projects become impossible.
Enterprise Example
Payment System:
PaymentGateway
↓
PayPal
Stripe
Crypto
UPI
Everything follows abstraction.
Software Layers Example
Controller
↓
Service
↓
Repository
↓
Database
Every layer abstracts another.
PocketMine Example
Plugin
↓
API
↓
Server
↓
Network
Bad Example
Socket::write();
Compression::compress();
Plugin developers should never do this.
Good Example
$player->sendMessage();
Common Beginner Mistakes
Confusing Encapsulation and Abstraction
Remember:
Abstraction = Hiding Complexity
Encapsulation = Hiding Data
Creating Too Many Layers
Bad:
Service
ServiceManager
ServiceController
ServiceHandler
Overengineering.
Exposing Internals
Bad:
public Socket $socket;
Best Practices
✅ Hide complexity.
✅ Expose simple APIs.
✅ Keep interfaces small.
✅ Use meaningful names.
✅ Do not leak implementation details.
Real Framework Examples
Laravel
User::find();
PocketMine
$player->teleport();
Symfony
$mailer->send();
DragonFly
session.SendMessage();
All are abstractions.
Mini Project
Create:
Payment System
Classes:
PaymentGateway
StripeGateway
PayPalGateway
CryptoGateway
Expose:
pay()
Hide everything else.
Exercises
Exercise 1
Create:
abstract class Vehicle
Methods:
start()
move()
stop()
Exercise 2
Create:
Animal
Dog
Cat
Bird
Exercise 3
Create:
Notifier
EmailNotifier
DiscordNotifier
SMSNotifier
Interview Questions
What is abstraction?
Hiding implementation details and exposing only required functionality.
Why is abstraction important?
Because it reduces complexity.
Which PHP features provide abstraction?
Abstract Classes
Interfaces
Difference between abstraction and encapsulation?
Abstraction → Hides complexity
Encapsulation → Hides data
Summary
Abstraction:
✅ Reduces complexity
✅ Makes systems easier
✅ Allows large architectures
✅ Enables frameworks
✅ Makes code maintainable
References
https://www.php.net/manual/en/language.oop5.abstract.php
https://www.php.net/manual/en/language.oop5.interfaces.php
Next Chapter
➡ Interfaces
➡ Abstract Classes
➡ Traits