diff options
author | Rasmus Lerdorf <rasmus@lerdorf.com> | 2015-01-30 22:57:40 -0800 |
---|---|---|
committer | Rasmus Lerdorf <rasmus@lerdorf.com> | 2015-01-30 22:57:40 -0800 |
commit | 74b85316ea06c17256e102483daa472f4b638221 (patch) | |
tree | abe0618e15fcfc93158b4239889485ffc70c4c1c /Zend/tests/foreach_003.phpt | |
parent | 130d7320b160443ca160ee6b3a19a034ee2c5ef1 (diff) | |
parent | 1e41295097576dbce6c197ddb7507c07ccae3cbe (diff) | |
download | php-git-dstogov-foreach.tar.gz |
Merge branch 'foreach' of https://github.com/dstogov/php-src into dstogov-foreachdstogov-foreach
Diffstat (limited to 'Zend/tests/foreach_003.phpt')
-rw-r--r-- | Zend/tests/foreach_003.phpt | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/Zend/tests/foreach_003.phpt b/Zend/tests/foreach_003.phpt new file mode 100644 index 0000000000..71b0f2a5a3 --- /dev/null +++ b/Zend/tests/foreach_003.phpt @@ -0,0 +1,71 @@ +--TEST-- +Iterator exceptions in foreach by value +--FILE-- +<?php +class IT implements Iterator { + private $n = 0; + private $count = 0; + private $trap = null; + + function __construct($count, $trap = null) { + $this->count = $count; + $this->trap = $trap; + } + + function trap($trap) { + if ($trap === $this->trap) { + throw new Exception($trap); + } + } + + function rewind() {$this->trap(__FUNCTION__); $this->n = 0;} + function valid() {$this->trap(__FUNCTION__); return $this->n < $this->count;} + function key() {$this->trap(__FUNCTION__); return $this->n;} + function current() {$this->trap(__FUNCTION__); return $this->n;} + function next() {$this->trap(__FUNCTION__); $this->n++;} +} + +foreach(['rewind', 'valid', 'key', 'current', 'next'] as $trap) { + $obj = new IT(3, $trap); + try { + // IS_CV + foreach ($obj as $key => $val) echo "$val\n"; + } catch (Exception $e) { + echo $e->getMessage() . "\n"; + } + unset($obj); + + try { + // IS_VAR + foreach (new IT(3, $trap) as $key => $val) echo "$val\n"; + } catch (Exception $e) { + echo $e->getMessage() . "\n"; + } + + try { + // IS_TMP_VAR + foreach ((object)new IT(2, $trap) as $key => $val) echo "$val\n"; + } catch (Exception $e) { + echo $e->getMessage() . "\n"; + } +} +?> +--EXPECT-- +rewind +rewind +rewind +valid +valid +valid +key +key +key +current +current +current +0 +next +0 +next +0 +next |