<?php
class Stack{
private $_data = array();
private $_end = null;
public function push($data){
if($this->_end === null){
$this->_end = 0;
}else{
$this->_end = $this->_end + 1;
}
$this->_data[$this->_end] = $data;
}
public function pop(){
if(empty($this->_data)){
return false;
}
$ret = $this->_data[$this->_end];
array_splice($this->_data, $this->_end);
$this->_end--;
return $ret;
}
public function getData(){
return $this->_data;
}
}
// test
$stack = new Stack();
$stack->push(0);
$pop_data = $stack->pop();
var_dump($pop_data, $stack->getData());
为什么不empty(0)
回来false
?我推0
到pop_data
. empty($this->_data)
应该返回false
,但测试的结果是int(0)
. 我很困惑。