Singly-linked queue in PHP 7 #2 I have improved this post to the code below. 

As always, any critique is much appreciated!

&lt;?php

class QueueNode {

    function __construct($item) {
        $this-&gt;item = $item;
        $this-&gt;next = null;
    }

    function __destruct() {
        $this-&gt;item = null;
    }

    function getItem() {
        return $this-&gt;item;
    }    
}

class QueueIterator implements Iterator {

    function __construct($queue) {
        $this-&gt;queue = $queue;
        $this-&gt;current_node = $queue-&gt;head;
    }

    public function current() {
        return $this-&gt;current_node-&gt;item;
    }

    public function key(): \scalar {
        return null;
    }

    public function next(): void {
        $this-&gt;current_node = $this-&gt;current_node-&gt;next;
    }

    public function rewind(): void {
        $this-&gt;current_node = $this-&gt;queue-&gt;head;
    }

    public function valid(): bool {
        return isset($this-&gt;current_node);
    }
}

class Queue implements IteratorAggregate {

    function __construct() {
        $this-&gt;head = null;
        $this-&gt;tail = null;
        $this-&gt;size = 0;
    }

    function __destruct() {
        $this-&gt;head = null;
        $this-&gt;tail = null;
    }

    function push($item) {
        if ($this-&gt;size === 0) {
            $this-&gt;head = $this-&gt;tail = new QueueNode($item);
        } else {
            $new_node = new QueueNode($item);
            $this-&gt;tail-&gt;next = $new_node;
            $this-&gt;tail = $new_node;
        }

        $this-&gt;size++;
    }

    function pop() {
        if ($this-&gt;size === 0) {
            throw new Exception("Popping from an empty queue.");
        }

        $ret = $this-&gt;head-&gt;getItem();
        $this-&gt;head = $this-&gt;head-&gt;next;

        if (--$this-&gt;size == 0) {
            $this-&gt;head = $this-&gt;tail = null;
        }

        return $ret;
    }

    function getFirst() {
        if  ($this-&gt;size() === 0) {
            throw new Exception("getFirst() on empty Queue.");
        }

        return $this-&gt;head-&gt;getItem();
    }

    function getLast() {
        if ($this-&gt;size() === 0) {
            throw new Exception("getLast() on empty Queue.");
        }

        return $this-&gt;tail-&gt;getItem();
    }

    function size() {
        return $this-&gt;size;
    }

    function isEmpty() {
        return size() === 0;
    }

    public function getIterator(): \Traversable {
        return new QueueIterator($this);
    }
}

$queue = new Queue();

for ($i = 1; $i &lt;= 10; $i++) {
    $queue-&gt;push($i);
}

echo "Iteration: ";

foreach ($queue as $item) {
    echo $item . " ";
}

echo "&lt;br&gt;Popping: ";

for ($i = 1; $i &lt;= 10; $i++) {
    echo $queue-&gt;getFirst() . ", " . $queue-&gt;pop() . " ";
}

echo "&lt;br&gt;Bye!";
?&gt;


Output

Iteration: 1 2 3 4 5 6 7 8 9 10 
Popping: 1, 1 2, 2 3, 3 4, 4 5, 5 6, 6 7, 7 8, 8 9, 9 10, 10 
Bye!
I see three major problems with your code:


You dont use PHP 7 type hinting / return types
You dont have any scopes on your methods
If you wont modify your classes in the future and/or you wont extend from these classes you should use final for your class declarations
you should always set declare(strict_types=1); at the beginning of every php script as long as there are no veeeery good reasons to avoid it. It will force you to write your code more typesafe or you will get fatal errors


I will give you an example for each point i mentioned

1. "You dont use PHP 7 type hinting / return types"

Before:

function push($item) {


After: 

function push(int $item): void


2. You dont have any scopes on your methods

Before:

function getItem() {


After:

public function getItem(): void


3. class declaration as final

Before:

class QueueNode


After:

final class QueueNode


Sidenote:  

Also you should use properties like private $item = null; instead of your constructor setting. Your IDE and also any other maintainer will have trouble finding bugs and/or understanding this code.