ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Stack.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23class Stack
24{
25 protected array $stack = [];
26
30 public function push($elem): void
31 {
32 $this->stack[] = $elem;
33 }
34
38 public function pop()
39 {
40 if (!$this->isEmpty()) {
41 $last_index = count($this->stack) - 1;
42 $elem = $this->stack[$last_index];
43 unset($this->stack[$last_index]);
44 $this->stack = array_values($this->stack); // re-index
45
46 return $elem;
47 }
48
49 return null;
50 }
51
55 public function top()
56 {
57 if (!$this->isEmpty()) {
58 return $this->stack[count($this->stack) - 1];
59 }
60
61 return null;
62 }
63
64 public function isEmpty(): bool
65 {
66 return !(bool) count($this->stack);
67 }
68
69 public function reset(): void
70 {
71 $this->stack = [];
72 }
73
74 public function count(): int
75 {
76 return count($this->stack);
77 }
78}