1: <?php
2: 3: 4:
5: class QStack extends QBaseClass {
6: private $objArray = array();
7:
8: public function Push($mixValue) {
9: array_push($this->objArray, $mixValue);
10:
11: return $mixValue;
12: }
13:
14: public function PushFirst($mixValue) {
15: if ($this->Size() > 0) {
16: $this->objArray = array_reverse($this->objArray);
17: array_push($this->objArray, $mixValue);
18:
19: $this->objArray = array_reverse($this->objArray);
20: } else
21: $this->objArray[0] = $mixValue;
22:
23: return $mixValue;
24: }
25:
26: public function Pop() {
27: if (!$this->IsEmpty())
28: return array_pop($this->objArray);
29: else
30: throw new QCallerException("Cannot pop off of an empty Stack");
31: }
32:
33: public function PopFirst() {
34: if (!$this->IsEmpty()) {
35: $this->objArray = array_reverse($this->objArray);
36: $mixToReturn = array_pop($this->objArray);
37: $this->objArray = array_reverse($this->objArray);
38: return $mixToReturn;
39: } else
40: throw new QCallerException("Cannot pop off of an empty Stack");
41: }
42:
43: public function Peek($intIndex) {
44: if (array_key_exists($intIndex, $this->objArray))
45: return $this->objArray[$intIndex];
46: else
47: throw new QIndexOutOfRangeException($intIndex, "Index on stack does not exist");
48: }
49:
50: public function PeekLast() {
51: if ($intCount = count($this->objArray))
52: return $this->objArray[$intCount - 1];
53: else
54: throw new QIndexOutOfRangeException($intCount - 1, "Stack is empty");
55: }
56:
57: public function IsEmpty() {
58: return (count($this->objArray) == 0);
59: }
60:
61: public function Size() {
62: return count($this->objArray);
63: }
64:
65: public function ConvertToArray() {
66: return $this->objArray;
67: }
68: }