PHP ArrayList Class


2008-06-25 Digg! icurtain Delcious icurtain
PHP ArrayList Class

PHP ArrayList Class - Having spent the last year working in Java, working with PHP now makes me cry and weep in anguish.. for many reasons, one of the main ones being its' complete inability to store more than 1 thing in anything other than an array

I have attempted to create a class that handles data in a marginally more friendly manner allowing you to interate through etc.. it's still a bit BETA but it speeds up my development times.. so here it is

  1. class ArrayList { 
  2.  
  3. //Copyright Mike Lang - icurtain.co.uk - please retain this header and give credit if used. 
  4. //This is a little class I've written to make my life easier and a little more like Java 
  5. //for those occasions when i just can't afford Java hosting 
  6. //It doesnt work the same as a Java arrayList.. it's just a high level aproximation of it 
  7. //if you have any comments or suggestions for improving or changing this class feel free to mail me 
  8. //mike [at] bluemedia dot co dot uk 
  9.  
  10.    //ARRAY LIST CLASS STARTS COUNTING AT 0!!!!! 
  11.    private $arrayList = array(); 
  12.    private $pointer = 0; 
  13.  
  14.    public function version(){ 
  15.    echo 'version 1.0'; 
  16.    } 
  17.     
  18.    public function add($item){ 
  19.       //$this->arrayList[sizeof($this->arrayList)] = $item; 
  20.       array_push($this->arrayList, $item); 
  21.    } 
  22.  
  23.    public function addAtPos($position, $item){ 
  24.       if($position < count($this->arrayList) && $position >= 0) 
  25.       { 
  26.       $this->add($item); 
  27.       $this->shift(count($this->arrayList)-1, $position); 
  28.       } 
  29.       else 
  30.       { 
  31.       throw new Exception('List out of bounds'); 
  32.       } 
  33.    } 
  34.  
  35.    public function getList(){ 
  36.       return $this->arrayList; 
  37.    } 
  38.  
  39.    public function hasValue(){ 
  40.       if(isset($this->arrayList[$this->pointer])) 
  41.          { 
  42.             return true; 
  43.          } 
  44.       else 
  45.          { 
  46.             return false; 
  47.          } 
  48.     } 
  49.  
  50.     public function hasNext(){ 
  51.       if($this->pointer <= count($this->arrayList)-1) 
  52.          { 
  53.             return true; 
  54.          } 
  55.       else 
  56.          { 
  57.             return false; 
  58.          } 
  59.     } 
  60.  
  61.  
  62.    public function next(){ 
  63.       if(isset($this->arrayList[$this->pointer])) 
  64.       { 
  65.          //return $this->arrayList[($this->pointer++)-1] = $value; 
  66.       $this->pointer++; 
  67.          return($this->arrayList[$this->pointer-1]); 
  68.       } 
  69.       else 
  70.       { 
  71.          return null; 
  72.       } 
  73.       } 
  74.  
  75.    public function shift($origin, $dest){ 
  76.          //wont shift from last element 
  77.          if($origin > count($this->arrayList) || $origin < 0 || $dest > count($this->arrayList) || $dest < 0) 
  78.          { 
  79.          throw new Exception('List out of bounds'); 
  80.          } 
  81.          if($origin > $dest) 
  82.             { 
  83.             $temp = $this->arrayList[$origin]; 
  84.             $this->shiftUp($origin, $dest); 
  85.             $this->arrayList[$dest] = $temp; 
  86.             } 
  87.             else 
  88.             { 
  89.             $temp = $this->arrayList[$origin]; 
  90.             $this->shiftDown($origin, $dest); 
  91.             $this->arrayList[$dest] = $temp; 
  92.             } 
  93.    } 
  94.  
  95.    private function shiftUp($origin, $dest) 
  96.          { 
  97.             for($i=$origin;$i>$dest;$i--) 
  98.                               { 
  99.                               $this->arrayList[$i] = $this->arrayList[$i-1]; 
  100.                               } 
  101.          } 
  102.  
  103.    private function shiftDown($origin, $dest) 
  104.          { 
  105.             for($i=$origin;$i<$dest;$i++) 
  106.                               { 
  107.                               $this->arrayList[$i] = $this->arrayList[$i+1]; 
  108.                               } 
  109.          } 
  110.  
  111.    public function remove($item){ 
  112.       if(array_key_exists($item, $this->arrayList)){ 
  113.          unset($this->arrayList[$item]); 
  114.       } 
  115.       else  
  116.       { 
  117.       throw new Exception('key not found'); 
  118.       } 
  119.    } 
  120.  
  121.    public function addArray($array){ 
  122.     foreach ($array as $item) { 
  123.          $this->add($item); 
  124.             } 
  125.    } 
  126.     
  127.    public function reverse(){ 
  128.       $this->arrayList = array_reverse($this->arrayList); 
  129.    } 
  130.  
  131.       public function size(){ 
  132.       return count($this->arrayList); 
  133.    } 
  134.  
  135.       public function reset(){ 
  136.       $this->pointer = 0; 
  137.    } 
  138.  
  139.    public function end(){ 
  140.       $this->pointer = count($this->arrayList) -1; 
  141.    } 
  142.  
  143. ?>

With it you can while($arrayList->hasNext()){} and do all kinds of other crazy things that will bring endless listing joy to your life - mail me if you have any improvement suggestions