1. What is SPL?
SPL (Standard PHP Library) is a collection of built-in data structures and interfaces designed to solve common problems efficiently.
Why use SPL?
- Better performance than custom arrays
- Clear intent (stack vs queue)
- Cleaner, more readable code
We’ll focus on:
SplStackSplQueue
2. Stack vs Queue (Concept)
Stack (LIFO)
- Last In, First Out
-
Examples:
- Undo/Redo
- Function call stack
- Browser back button
Queue (FIFO)
- First In, First Out
-
Examples:
- Job processing
- Task scheduling
- Message queues
3. SplStack
3.1 Creating a Stack
$stack = new SplStack();
3.2 Pushing Elements (push)
$stack->push("A");
$stack->push("B");
$stack->push("C");
Stack state:
Top -> C
B
Bottom-> A
3.3 Popping Elements (pop)
echo $stack->pop(); // C
Explanation:
- Removes and returns the top element.
3.4 Peeking Top Element (top)
echo $stack->top(); // B
- Reads top element without removing it.
3.5 Checking if Stack is Empty
if ($stack->isEmpty()) {
echo "Stack is empty";
}
3.6 Counting Elements
echo $stack->count(); // number of elements
3.7 Iterating a Stack
Important:
-
SplStackiterates from top to bottom by default.
foreach ($stack as $item) {
echo $item . PHP_EOL;
}
3.8 Real-World Example: Undo Feature
$undoStack = new SplStack();
$undoStack->push("Typed 'Hello'");
$undoStack->push("Typed 'World'");
$undoStack->push("Deleted 'World'");
// User presses undo
echo $undoStack->pop(); // Deleted 'World'
4. SplQueue
4.1 Creating a Queue
$queue = new SplQueue();
4.2 Enqueue (add to queue)
$queue->enqueue("Job1");
$queue->enqueue("Job2");
$queue->enqueue("Job3");
Queue state:
Front -> Job1, Job2, Job3 -> Back
4.3 Dequeue (remove from queue)
echo $queue->dequeue(); // Job1
- Removes and returns the first element.
4.4 Peeking Front Element
echo $queue->bottom(); // Job2
Explanation:
-
bottom()→ front of the queue -
top()→ back of the queue
4.5 Checking if Queue is Empty
if ($queue->isEmpty()) {
echo "Queue is empty";
}
4.6 Counting Elements
echo $queue->count();
4.7 Iterating a Queue
foreach ($queue as $job) {
echo $job . PHP_EOL;
}
Iteration order:
- Front → Back
4.8 Real-World Example: Job Processing
$jobQueue = new SplQueue();
// Add jobs
$jobQueue->enqueue("Send Email");
$jobQueue->enqueue("Generate Invoice");
$jobQueue->enqueue("Notify User");
// Process jobs
while (!$jobQueue->isEmpty()) {
$job = $jobQueue->dequeue();
echo "Processing: $job" . PHP_EOL;
}
5. Stack vs Queue Summary
| Feature | SplStack | SplQueue |
|---|---|---|
| Order | LIFO | FIFO |
| Add | push() | enqueue() |
| Remove | pop() | dequeue() |
| Typical Use | Undo, recursion | Jobs, tasks |
6. Common Mistakes
❌ Using arrays instead of SPL
array_push($stack, $value); // Not recommended
Why?
- Slower
- Less expressive
- Easy to misuse order
Use SPL instead.
❌ Confusing top() and bottom() in Queue
-
bottom()→ front -
top()→ back
7. When to Use SPL in Laravel / PHP Apps
Use SplStack when:
- Undo/redo
- Nested operations
- Temporary reversal logic
Use SplQueue when:
- Sequential jobs
- Rate-limited API calls
- Background task pipelines
Laravel internal usage:
- Symfony (Laravel’s base) uses SPL structures heavily.
8. Performance Note
- SPL structures are implemented in C
- Faster than PHP arrays for stack/queue operations
- Less memory overhead for frequent push/pop
9. Quick Cheat Sheet
// Stack
$stack->push($item);
$stack->pop();
$stack->top();
// Queue
$queue->enqueue($item);
$queue->dequeue();
$queue->bottom();
Top comments (0)