Implementação de uma lista em PHP. Esta classe está pronta a usar. Permite aceder às duas extremidades da lista, bem como a qualquer outro ponto da lista, pelo que pode ser usada como pilha ou fila de espera.
<?php /** *************************** * Author: Carlos Correia * * Version: 0.1 * * Date: 06/11/2007 * *************************** */ class DefaultArray{ protected $array; /** * Construct an empty array */ function __construct(){ $this->array = array(); } /** * Add an element to the end of array * @param object $element */ function addLast($element){ $this->array[]=$element; } /** * Add an element to the begin of array * @param object $element */ function addFirst($element){ array_unshift($this->array,$element); } /** * Add element in array into position * indicate by the index argument * @param int $index * @param object $element */ function addElementAt($index,$element){ if($index>=0 and $index<=count($this->array)) array_splice($this->array,$index,0,$element); else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); if($index<0) throw new Exception('INDEX IS NEGATIVE'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * replace an element in array into position indicate * by the index argument * @param int $index * @param object $element */ function setElementAt($index,$element){ if (isset($this->array[$index])) { $this->array[$index]=$element; } else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); if($index<0) throw new Exception('INDEX IS NEGATIVE'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Return an element in array, indicate by the * index argument * @param int $index * @return object */ function getElementAt($index){ if(isset($this->array[$index])) return $this->array[$index]; else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); if($index<0) throw new Exception('INDEX IS NEGATIVE'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Return the first element in array * @return object */ function getFirst(){ if (isset($this->array[0])) return $this->array[0]; else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * return the last element in array * @return object */ function getLast(){ $last_position=count($this->array)-1; if (isset($this->array[$last_position])) return $this->array[$last_position]; else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Remove the first element in array * @return object */ function removeFirst(){ if (isset($this->array[0])) return array_shift($this->array); else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Remove the last element in aray * @return object */ function removeLast(){ $last_position=count($this->array)-1; if ($last_position>=0) return array_pop($this->array); else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Remove an element in position indicate by * the index argument * @param int $index * @return removed element */ function removeElementAt($index){ if(isset($this->array[$index])){ $removed = array_splice($this->array,$index,1); return $removed[0]; } else try { if(count($this->array)==0) throw new Exception('EMPTY ARRAY'); if($index<0) throw new Exception('INDEX IS NEGATIVE'); else throw new Exception('POSITION NOT FOUND'); }catch (Exception $e){echo $e; exit;} } /** * Append currente array with other array * given by argument * @param DefaultArray or Array */ function appendArray($array){ if (is_a($array,'DefaultArray')) $this->array=array_merge($this->array,$array->getArray()); elseif(is_array($array)) $this->array=array_merge($this->array,$array); else try { throw new Exception('ARGUMENT IS INVALID'); }catch (Exception $e){echo $e; exit;} } /** * @return int size of array */ function getSize(){ return count($this->array); } /** * write a tree of values in array * @return string */ function toString(){ return var_export($this->array,true); } /** * Return array * @return array */ function getArray(){ return $this->array; } } ?>