- 将原 parseForeach 方法拆分为 parseForeach 和 parseForeachArray 两个方法 - 添加对 Iterator 和 IteratorAggregate 接口的支持 - 实现对象属性遍历的备用方案,使用 get_object_vars 函数 - 添加新的测试用例 iterators_004.phpt 验证迭代器功能 - 添加新的测试用例 iterators_005.phpt 验证 Traversable 接口实现检查 - 为对象迭代创建临时变量并设置正确的类型标识pull/1/head
parent
749b4ec00d
commit
715abf458d
4 changed files with 113 additions and 25 deletions
@ -0,0 +1,56 @@ |
|||||||
|
--TEST-- |
||||||
|
ZE2 iterators must be implemented |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class c1 {} |
||||||
|
class c2 { |
||||||
|
|
||||||
|
public $max = 3; |
||||||
|
public $num = 0; |
||||||
|
|
||||||
|
function current() { |
||||||
|
echo __METHOD__ . "\n"; |
||||||
|
return $this->num; |
||||||
|
} |
||||||
|
function next(): void { |
||||||
|
echo __METHOD__ . "\n"; |
||||||
|
$this->num++; |
||||||
|
} |
||||||
|
function valid(): bool { |
||||||
|
echo __METHOD__ . "\n"; |
||||||
|
return $this->num < $this->max; |
||||||
|
} |
||||||
|
function key(): mixed { |
||||||
|
echo __METHOD__ . "\n"; |
||||||
|
switch($this->num) { |
||||||
|
case 0: return "1st"; |
||||||
|
case 1: return "2nd"; |
||||||
|
case 2: return "3rd"; |
||||||
|
default: return "???"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
function main() { |
||||||
|
echo "1st try\n"; |
||||||
|
$obj = new c1(); |
||||||
|
|
||||||
|
foreach($obj as $w) { |
||||||
|
echo "object:$w\n"; |
||||||
|
} |
||||||
|
|
||||||
|
echo "2nd try\n"; |
||||||
|
$obj = new c2(); |
||||||
|
|
||||||
|
foreach($obj as $v => $w) { |
||||||
|
echo "object:$v=>$w\n"; |
||||||
|
} |
||||||
|
|
||||||
|
print "Done\n"; |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
1st try |
||||||
|
2nd try |
||||||
|
object:max=>3 |
||||||
|
object:num=>0 |
||||||
|
Done |
||||||
@ -0,0 +1,20 @@ |
|||||||
|
--TEST-- |
||||||
|
ZE2 iterators cannot implement Traversable alone |
||||||
|
--SKIPIF-- |
||||||
|
<?php die('skip'); ?> |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class test implements Traversable { |
||||||
|
} |
||||||
|
function main() { |
||||||
|
$obj = new test; |
||||||
|
|
||||||
|
foreach($obj as $v); |
||||||
|
|
||||||
|
print "Done\n"; |
||||||
|
/* the error doesn't show the filename but 'Unknown' */ |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
Fatal error: Class test must implement interface Traversable as part of either Iterator or IteratorAggregate in %s on line %d |
||||||
Loading…
Reference in new issue