2013-04-23 208 views
-3
<?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_dataempty($this->_data)應返回false,但測試結果爲int(0)。我感到困惑。爲什麼不爲空(0)返回false?

+3

我看不出這是對的。 http://php.net/manual/en/function.empty.php說,空返回一個布爾,而不是一個int。 – Patashu 2013-04-23 03:04:15

+0

@Patashu我不認爲他實際上質疑php函數,但他與空()renrenshang一起使用的函數我認爲這是因爲嘗試使其他語句中的代碼的其餘部分後運行的功能的其餘部分 – Breezer 2013-04-23 03:07:32

+0

嘗試'var_dump($此 - > _數據)'。 – Raptor 2013-04-23 03:08:24

回答

0

$ this - > _你的代碼中的數據是一個數組。

替換這個(評估傳遞零值),您的線路:

$this->_data[$this->_end]; 
+0

非常感謝你! – renrenshang 2013-04-24 02:10:04

相關問題