*/ class Stack { /** * Stack data. * @var mixed[] */ protected $stack; /** * Create a new stack. */ public function __construct() { $this->stack = []; } /** * Count how many items this stack contains. * * @return int */ public function count(): int { return sizeof($this->stack); } /** * Get all items in this stack. * * @return mixed[] */ public function get(): array { return $this->stack; } /** * Pop an item off of this stack. * * @param int $offset offset from the end to pop if not the last element * @return mixed */ public function pop(int $offset = 0) { if (sizeof($this->stack) < $offset + 1) { throw new \Exception('Stack underflow!'); } return array_splice($this->stack, -1 * ($offset + 1), 1)[0]; } /** * Push an item onto the end of the stack. * * @param mixed $value * @return void */ public function push($value): void { array_push($this->stack, $value); } }