update page now

The SplStack class

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

Introduction

The SplStack class provides the main functionalities of a stack implemented using a doubly linked list by setting the iterator mode to SplDoublyLinkedList::IT_MODE_LIFO.

Class synopsis

class SplStack extends SplDoublyLinkedList {
/* Inherited constants */
/* Inherited methods */
public function SplDoublyLinkedList::add(int $index, mixed $value): void
public function SplDoublyLinkedList::count(): int
public function SplDoublyLinkedList::key(): int
public function SplDoublyLinkedList::next(): void
public function SplDoublyLinkedList::offsetExists(int $index): bool
public function SplDoublyLinkedList::offsetGet(int $index): mixed
public function SplDoublyLinkedList::offsetSet(?int $index, mixed $value): void
public function SplDoublyLinkedList::offsetUnset(int $index): void
public function SplDoublyLinkedList::pop(): mixed
public function SplDoublyLinkedList::prev(): void
public function SplDoublyLinkedList::push(mixed $value): void
public function SplDoublyLinkedList::top(): mixed
public function SplDoublyLinkedList::unshift(mixed $value): void
public function SplDoublyLinkedList::valid(): bool
}

Examples

Example #1 SplStack example

<?php
$q
= new SplStack();
$q[] = 1;
$q[] = 2;
$q[] = 3;
foreach (
$q as $elem) {
echo
$elem."\n";
}
?>

The above example will output:

3
2
1

add a note

User Contributed Notes 2 notes

up
42
lsroudi at gmail dot com
12 years ago
the SplStack is  simply a SplDoublyLinkedList with  an iteration mode IT_MODE_LIFO and IT_MODE_KEEP
up
8
lincoln dot du dot j at gmail dot com
8 years ago
<?php
//SplStack Mode is LIFO (Last In First Out)
  
$q = new SplStack();

$q[] = 1;
$q[] = 2;
$q[] = 3;
$q->push(4);
$q->add(4,5);

$q->rewind();
while($q->valid()){
    echo $q->current(),"\n";
    $q->next();
}
?>

Output
5
4
3
2
1
To Top