Speed up development with full-stack environments for every branch.

Learn More

How to implement the SplStack class [PHP]

295 Runs 1191 Views 398 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP SPL - How to implement the SplStack class
 */

//Think of the stack as an array reversed, where the last element has index zero
$stack = new SplStack();
//push method adds the element at the begining of the stack
$stack->push('a');
$stack->push('b');
$stack->push('c');

//rewind iterator back to the start
$stack->rewind();

//pop method removes an element from the end of the stack
$stack->pop();

//iterate the stack and echo each element
while ($stack->valid()) {
    echo $stack->current(), PHP_EOL;
    $stack->next();
}
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).