<?php
// LIFO by default
$list = new SplStack();
try {
// unlike SplDoublyLinkedList you can't change direction
$list->setIteratorMode(SplStack::IT_MODE_FIFO);
}
catch(RuntimeException $e) {
echo $e->getMessage() . PHP_EOL;
}
try {
// but you can't change KEEP to DELETE either
// Is the PHP documentation wrong?
$list->setIteratorMode(SplStack::IT_MODE_DELETE);
}
catch(RuntimeException $e) {
echo $e->getMessage() . PHP_EOL;
}
// LIFO needs to be readded again to prevent an exception
$list->setIteratorMode(
SplStack::IT_MODE_LIFO |
SplStack::IT_MODE_DELETE
);
// adding elements with push or unshift
$list->push(1);
$list->push(10);
$list->push(100);
foreach ($list as $element) {
echo $element . PHP_EOL;
}
echo 'count: ' . count($list);