Welcome to the practical segment of our PHP programming journey! Today, we'll apply the knowledge from past lessons to solve two practice problems using PHP's data structures: queues
with SplQueue
, deques
using SplDoublyLinkedList
, and associative arrays
for managing ordered data.
Consider an event-driven system, like a restaurant. Orders arrive, and they must be handled in the order they were received, following the First In, First Out (FIFO) principle. This principle makes it a perfect scenario for a queue
implementation using PHP's SplQueue
.
php1<?php 2 3class Queue 4{ 5 private $buffer; 6 7 public function __construct() 8 { 9 // Initializing an empty queue 10 $this->buffer = new SplQueue(); 11 } 12 13 // Adding (enqueueing) an item to the queue 14 public function enqueue($val) 15 { 16 $this->buffer->enqueue($val); 17 } 18 19 // Removing (dequeuing) an item from the queue 20 public function dequeue() 21 { 22 if ($this->isEmpty()) { 23 throw new UnderflowException("Queue is empty"); 24 } 25 return $this->buffer->dequeue(); 26 } 27 28 // Checking if the queue is empty 29 public function isEmpty() 30 { 31 return $this->buffer->isEmpty(); 32 } 33 34 // Checking the size (number of items) in the queue 35 public function size() 36 { 37 return $this->buffer->count(); 38 } 39} 40 41$restaurantQueue = new Queue(); 42$restaurantQueue->enqueue("Order 1"); 43$restaurantQueue->enqueue("Order 2"); 44 45echo "Dequeued: " . $restaurantQueue->dequeue() . PHP_EOL; 46echo "Dequeued: " . $restaurantQueue->dequeue() . PHP_EOL;
This code demonstrates the creation and operation of a Queue
class, which utilizes PHP's SplQueue
to efficiently implement a queue. The Queue
class includes methods to enqueue
(add) an item, dequeue
(remove) an item, check if the queue is empty, and return the queue's size. Enqueue operations add an item to the queue (simulating the arrival of a new order), while dequeue operations remove an item from the front (simulating the serving of an order), maintaining the First In, First Out (FIFO) principle.
We've mimicked a real-world system by implementing a queue
using PHP's SplQueue
. The enqueuing of an item adheres to the FIFO principle, similar to the action of receiving a new order at a restaurant. The dequeuing serves an order, reflecting the preparation and delivery of the order.
For the second problem, envision a leaderboard for a video game. Players with their scores can be represented as associative arrays, sorted by score for easy and efficient access.
php1<?php 2 3class Player 4{ 5 public $name; 6 public $score; 7 8 public function __construct($name, $score) 9 { 10 $this->name = $name; 11 $this->score = $score; 12 } 13 14 public function __toString() 15 { 16 return "({$this->name}, {$this->score})"; 17 } 18} 19 20$scores = [ 21 new Player("John", 900), 22 new Player("Doe", 1000) 23]; 24 25// Sorting the array using custom comparison function 26usort($scores, function($a, $b) { 27 return $b->score <=> $a->score ?: strcmp($a->name, $b->name); 28}); 29 30// Print sorted leaderboard 31foreach ($scores as $player) { 32 echo $player . PHP_EOL; 33}
This code snippet introduces a Player
class for representing players in a video game. Players are stored in an indexed array, which is then sorted using the usort
function with a custom comparison callback. The array is sorted by score (primary) and name (secondary), enabling a leaderboard functionality in PHP.
In our leaderboard system, we used a Player
custom class and leveraged PHP's usort
function with a custom callback for sorting. This approach efficiently achieves ordered key-value pair behavior with associative arrays in PHP, simulating the ordered nature of a sorted dictionary.
We've successfully employed PHP's built-in data structures — SPLQueue
for queues, SPLDoublyLinkedList
for deques, and associative arrays with custom sorting — to solve real-world problems. This hands-on approach enables us to better understand these concepts. More exercises to cement your understanding are coming up next. Happy coding!