Skip to content

Instantly share code, notes, and snippets.

@esase
Last active May 29, 2016 08:51
Show Gist options
  • Save esase/7a0c17065424b5c9f63b9e6e06dfd2ee to your computer and use it in GitHub Desktop.
Save esase/7a0c17065424b5c9f63b9e6e06dfd2ee to your computer and use it in GitHub Desktop.
Queue [data type]
<?php
class Queue
{
/**
* Items
*
* @var array
*/
protected $items = [];
/**
* Enqueue
*
* @param mixed $value
* @return void
*/
public function enqueue($value)
{
$this->items[] = $value;
}
/**
* Dequeue
*
* @return mixed
* @throws Exception
*/
public function dequeue()
{
if (!$this->items) {
throw new Exception('There are no any items in queue');
}
$currentItemValue = current($this->items);
unset($this->items[key($this->items)]);
return $currentItemValue;
}
}
$queue = new Queue();
// fill the queue
foreach (range(0, 15) as $value) {
$queue->enqueue($value);
}
// extract from the queue
foreach (range(0, 15) as $value) {
echo $queue->dequeue() . '<br>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment