fork download
  1. <?php
  2. class Foo {
  3. public $name;
  4. public $type;
  5.  
  6. public function __construct($name, $type) {
  7. $this->name = $name;
  8. $this->type = $type;
  9. }
  10.  
  11. public static function searchZeroType($o) {
  12. return ($o->type == 0);
  13. }
  14.  
  15. public static function doubleType($o) {
  16. $r = $o->type;
  17. $r= $r * 2;
  18.  
  19. return $r;
  20. }
  21. }
  22.  
  23. class SplObjectStorageExtended extends SplObjectStorage {
  24.  
  25. public function objectFilter(callable $c) {
  26. $s = new SplObjectStorageExtended();
  27.  
  28. foreach($this as $object) {
  29. if ($c($object)) {
  30. $s->attach($object);
  31. }
  32. }
  33.  
  34. return $s;
  35. }
  36.  
  37. public function objectMap(callable $c) {
  38. $array = array();
  39.  
  40. foreach($this as $object) {
  41. $array[] = $c($object);
  42. }
  43.  
  44. return $array;
  45. }
  46.  
  47. public function objectWalk(callable $c) {
  48. foreach ($this as $object) {
  49. $c($object);
  50. }
  51. }
  52. }
  53.  
  54. $a = new SplObjectStorageExtended();
  55.  
  56. $a->attach(new Foo('first', 1));
  57. $a->attach(new Foo('second', 1));
  58. $a->attach(new Foo('third', 0));
  59. $a->attach(new Foo('fourth', 1));
  60.  
  61.  
  62. $search = $a->objectFilter(array('Foo', 'searchZeroType'));
  63. $double = $a->objectMap(array('Foo', 'doubleType'), $a);
  64.  
  65. $a->objectWalk(function($o) {echo $o->name . " ";});
  66.  
  67. //print_r($search);
Success #stdin #stdout 0.02s 24400KB
stdin
Standard input is empty
stdout
first second third fourth