Collections with SPL
SPL Stack
SplStack removes the most recently pushed item first.
last in first out
Stacks are useful when the latest item should be processed before earlier items.
SPL Stack
spl_stack.php
Replay: real traced execution (multi-file project)
<?php
$base = "draft";
$stack = new SplStack();
$stack->push($base);
$stack->push("review");
$top = $stack->pop();
$next = $stack->top();
$remaining = $stack->count();
echo "base=" . $base . "\n";
echo "top=" . $top . "\n";
echo "next=" . $next . "\n";
echo "remaining=" . $remaining . "\n";
<?php
$base = "note";
$stack = new SplStack();
$stack->push($base);
$stack->push("review");
$top = $stack->pop();
$next = $stack->top();
$remaining = $stack->count();
echo "base=" . $base . "\n";
echo "top=" . $top . "\n";
echo "next=" . $next . "\n";
echo "remaining=" . $remaining . "\n";
<?php
$base = "ticket";
$stack = new SplStack();
$stack->push($base);
$stack->push("review");
$top = $stack->pop();
$next = $stack->top();
$remaining = $stack->count();
echo "base=" . $base . "\n";
echo "top=" . $top . "\n";
echo "next=" . $next . "\n";
echo "remaining=" . $remaining . "\n";
$base ← draft, $stack ← SplStack, $top ← review, $next ← draft
1<?php2$base→ draft = "draft"; //@base="note", "ticket"3$stack→ SplStack = new SplStack();4$stackSplStack->push($basedraft);5$stackSplStack->push("review");6$top→ review = $stackSplStack->pop();7$next→ draft = $stackSplStack->top();8$remaining→ 1 = $stackSplStack->count();910echo "base=" . $basedraft . "\n";11echo "top=" . $topreview . "\n";12echo "next=" . $nextdraft . "\n";13echo "remaining=" . $remaining1 . "\n";outputbase=draft top=review next=draft remaining=1
$base ← note, $stack ← SplStack, $top ← review, $next ← note, $remaining ← 1
1<?php2$base→ note = "note";3$stack→ SplStack = new SplStack();4$stackSplStack->push($basenote);5$stackSplStack->push("review");6$top→ review = $stackSplStack->pop();7$next→ note = $stackSplStack->top();8$remaining→ 1 = $stackSplStack->count();910echo "base=" . $basenote . "\n";11echo "top=" . $topreview . "\n";12echo "next=" . $nextnote . "\n";13echo "remaining=" . $remaining1 . "\n";outputbase=note top=review next=note remaining=1
$base ← ticket, $stack ← SplStack, $top ← review, $next ← ticket
1<?php2$base→ ticket = "ticket";3$stack→ SplStack = new SplStack();4$stackSplStack->push($baseticket);5$stackSplStack->push("review");6$top→ review = $stackSplStack->pop();7$next→ ticket = $stackSplStack->top();8$remaining→ 1 = $stackSplStack->count();910echo "base=" . $baseticket . "\n";11echo "top=" . $topreview . "\n";12echo "next=" . $nextticket . "\n";13echo "remaining=" . $remaining1 . "\n";outputbase=ticket top=review next=ticket remaining=1