---
title: "PHP实现的栈"
author: "keshion"
date: "2012-11-16T13:44:29Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2012-11-16T13:44:29Z"
id: 1727808
url: "https://keshion.iteye.com/blog/1727808"
---

```
<?php

class Stack{
    private $_data = array();
    private $_end = null;

    public function push($data){
        if($this->_end === null){
            $this->_end = 0;
        }else{
            $this->_end++;
        }
        $this->_data[$this->_end] = $data;
    }

    public function pop(){
        if(empty($this->_data)){return false;}
        
        $ret = $this->_data[$this->_end];

        //array_pop($this->_data);
        array_splice($this->_data, $this->_end);

        $this->_end--;
        
        return $ret;
    }

    public function get_data(){
        return $this->_data;
    }

}

$stack = new Stack();
$stack->push('a');
$stack->push('b');
$stack->push('c');
$stack->pop();
print_r($stack->get_data());
```