<?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_splice ( $this ->_data, $this ->_end); $this ->_end--; return $ret ; } public function getData(){ return $this ->_data; } } $stack = new stack(); $stack ->push( 'chuan' ); $stack ->push( 'shan' ); $stack ->push( 'jia' ); $pop_data = $stack ->pop(); var_dump( $pop_data , $stack ->getData()); |
结果: string(3) "jia" array (2) { [0]=> string(5) "chuan" [1]=> string(4) "shan" } |