PHP Stack Implementation

Change your constructor as follows:

<?php

class Stack {

    protected $stack;
    protected $limit;

    public function __construct($limit = 10, $initial = array()) {
        // initialize the stack
        $this->stack = $initial;
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
            throw new RunTimeException('Stack is empty!');
        } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }

}

/**
 * This'll work as expected.
 */
$stack = new Stack();
$stack->push(1);
$stack->push(2);
$stack->push(3);
$stack->push(4);
$stack->push(5);

/**
 * And this too.
 */
$stack = new Stack(10, array(1, 2, 3, 4, 5));

Just FYI, PHP has array_push (http://php.net/manual/en/function.array-push.php) and array_pop (http://us3.php.net/array_pop) implementations.


Here's the implementation of the correct stack class. To correctly initialize array to the value of a stack, you have to reverse the values of that array like this:

class Stack
{
    protected $stack;
    protected $limit;

    public function __construct($values = array(),$limit = 10) {
        // initialize the stack
        $this->stack = array_reverse($values);
        // stack can only contain this many items
        $this->limit = $limit;
    }

    public function push($item) {
        // trap for stack overflow
        if (count($this->stack) < $this->limit) {
            // prepend item to the start of the array
            array_unshift($this->stack, $item);
        } else {
            throw new RunTimeException('Stack is full!');
        }
    }

    public function pop() {
        if ($this->isEmpty()) {
            // trap for stack underflow
          throw new RunTimeException('Stack is empty!');
      } else {
            // pop item from the start of the array
            return array_shift($this->stack);
        }
    }

    public function top() {
        return current($this->stack);
    }

    public function isEmpty() {
        return empty($this->stack);
    }
}

Happy coding!


Stack using php(Procedural approach)

$data_array = [];
$top = -1;

function push($top, $item, $data_array){
    global $top,$data_array;
    $top++;
    $data_array[$top] = $item;
    return $data_array;
}


function pop(){
    global $top,$data_array;
    if($top < 0)
        return -1;
    $top_item = $data_array[$top];
    unset($data_array[$top]);
    $top--;
    return $top_item;
}
push($top, 1, $data_array);
push($top, 2, $data_array);
push($top, 3, $data_array)

pop();