Advertisement
Guest User

Untitled

a guest
Oct 2nd, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. function foo($t) {
  2. $result;
  3. foreach($t as $val) {
  4. $result = dosomething($result, $val);
  5. }
  6. return $result;
  7. }
  8.  
  9. function foo(Traversable $t) {
  10.  
  11. Argument 1 passed to foo() must implement interface Traversable, array given
  12.  
  13. function foo(array $a) {}
  14. function bar(Traversable $a) {}
  15. function foobar($a) {}
  16.  
  17. $array = array(1,2,3)
  18. $traversable = new MyTraversableObject();
  19.  
  20. foo($array);
  21. foo(iterator_to_array($traversable));
  22.  
  23. bar(new ArrayIterator($array));
  24. bar($traversable);
  25.  
  26. foobar($array);
  27. foobar($traversable);
  28.  
  29. function MyFunction($traversable)
  30. {
  31. if(!$traversable instanceof Traversable && !is_array($traversable))
  32. {
  33. throw new InvalidArgumentException(sprintf(
  34. 'Myfunction($traversable = %s): Invalid argument $traversable.'
  35. ,var_export($traversable, true)
  36. ));
  37. }
  38. }
  39.  
  40. public function MyMethod($traversable)
  41. {
  42. if(!$traversable instanceof Traversable && !is_array($traversable))
  43. {
  44. throw new InvalidArgumentException(sprintf(
  45. '%s::MyMethod($traversable): Invalid argument $traversable of type `%s`.'
  46. ,get_class($this)
  47. ,gettype($traversable)
  48. ));
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement