Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Last active August 29, 2015 14:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/e75d7c23e4d81156b96c to your computer and use it in GitHub Desktop.
Save chrisguitarguy/e75d7c23e4d81156b96c to your computer and use it in GitHub Desktop.
Can't throw in generator functions? You can, but they don't throw until iteration starts.
<?php
function gen($x)
{
if (!is_int($x)) {
throw new \InvalidArgumentException;
}
foreach (range(0, $x) as $i) {
yield $i;
}
}
gen('nope'); // no exception
foreach (gen('still nope') as $i) {
// this loop will throw
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment