Posted By

dbug13 on 03/11/09


Tagged

class php textmate set


Versions (?)

a Set class that maintains an array of unique items


 / Published in: PHP
 

  1. <?php
  2. class Set
  3. {
  4. protected $_storage_array;
  5.  
  6. function __constructor()
  7. {
  8. $this->_storage_array = array();
  9. }
  10.  
  11. function add($value)
  12. {
  13. $this->_storage_array[$value] = $value;
  14. }
  15.  
  16. function hasValue($value)
  17. {
  18. return in_array($value, array_keys($this->_storage_array));
  19. }
  20.  
  21. function toArray()
  22. {
  23. return array_keys($this->_storage_array);
  24. }
  25. }
  26.  
  27. $data = new Set;
  28. $data->add("Sarah");
  29. $data->add("Jamie");
  30. $data->add("Phil");
  31. $data->add("Jamie");
  32.  
  33. print("Has Value Jamie? " . ($data->hasValue("Jamie") ? 'true' : 'false')) . PHP_EOL;
  34. print_r($data->toArray());
  35.  
  36. /*
  37. Returns the following:
  38.  
  39. Has Value Jamie? true
  40. Array
  41. (
  42.   [0] => Sarah
  43.   [1] => Jamie
  44.   [2] => Phil
  45. )
  46. */
  47. ?>

Report this snippet  

You need to login to post a comment.