Annotation of embedaddon/php/tests/classes/iterators_001.phpt, revision 1.1

1.1     ! misho       1: --TEST--
        !             2: ZE2 iterators and foreach
        !             3: --SKIPIF--
        !             4: <?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?>
        !             5: --FILE--
        !             6: <?php
        !             7: class c_iter implements Iterator {
        !             8: 
        !             9:        private $obj;
        !            10:        private $num = 0;
        !            11: 
        !            12:        function __construct($obj) {
        !            13:                echo __METHOD__ . "\n";
        !            14:                $this->num = 0;
        !            15:                $this->obj = $obj;
        !            16:        }
        !            17:        function rewind() {
        !            18:        }
        !            19:        function valid() {
        !            20:                $more = $this->num < $this->obj->max;
        !            21:                echo __METHOD__ . ' = ' .($more ? 'true' : 'false') . "\n";
        !            22:                return $more;
        !            23:        }
        !            24:        function current() {
        !            25:                echo __METHOD__ . "\n";
        !            26:                return $this->num;
        !            27:        }
        !            28:        function next() {
        !            29:                echo __METHOD__ . "\n";
        !            30:                $this->num++;
        !            31:        }
        !            32:        function key() {
        !            33:                echo __METHOD__ . "\n";
        !            34:                switch($this->num) {
        !            35:                        case 0: return "1st";
        !            36:                        case 1: return "2nd";
        !            37:                        case 2: return "3rd";
        !            38:                        default: return "???";
        !            39:                }
        !            40:        }
        !            41: }
        !            42:        
        !            43: class c implements IteratorAggregate {
        !            44: 
        !            45:        public $max = 3;
        !            46: 
        !            47:        function getIterator() {
        !            48:                echo __METHOD__ . "\n";
        !            49:                return new c_iter($this);
        !            50:        }
        !            51: }
        !            52: 
        !            53: echo "===Array===\n";
        !            54: 
        !            55: $a = array(0,1,2);
        !            56: foreach($a as $v) {
        !            57:        echo "array:$v\n";
        !            58: }
        !            59: 
        !            60: echo "===Manual===\n";
        !            61: $t = new c();
        !            62: for ($iter = $t->getIterator(); $iter->valid(); $iter->next()) {
        !            63:        echo $iter->current() . "\n";
        !            64: }
        !            65: 
        !            66: echo "===foreach/std===\n";
        !            67: foreach($t as $v) {
        !            68:        echo "object:$v\n";
        !            69: }
        !            70: 
        !            71: echo "===foreach/rec===\n";
        !            72: foreach($t as $v) {
        !            73:        foreach($t as $w) {
        !            74:                echo "double:$v:$w\n";
        !            75:        }
        !            76: }
        !            77: 
        !            78: echo "===foreach/key===\n";
        !            79: foreach($t as $i => $v) {
        !            80:        echo "object:$i=>$v\n";
        !            81: }
        !            82: 
        !            83: print "Done\n";
        !            84: exit(0);
        !            85: ?>
        !            86: --EXPECT--
        !            87: ===Array===
        !            88: array:0
        !            89: array:1
        !            90: array:2
        !            91: ===Manual===
        !            92: c::getIterator
        !            93: c_iter::__construct
        !            94: c_iter::valid = true
        !            95: c_iter::current
        !            96: 0
        !            97: c_iter::next
        !            98: c_iter::valid = true
        !            99: c_iter::current
        !           100: 1
        !           101: c_iter::next
        !           102: c_iter::valid = true
        !           103: c_iter::current
        !           104: 2
        !           105: c_iter::next
        !           106: c_iter::valid = false
        !           107: ===foreach/std===
        !           108: c::getIterator
        !           109: c_iter::__construct
        !           110: c_iter::valid = true
        !           111: c_iter::current
        !           112: object:0
        !           113: c_iter::next
        !           114: c_iter::valid = true
        !           115: c_iter::current
        !           116: object:1
        !           117: c_iter::next
        !           118: c_iter::valid = true
        !           119: c_iter::current
        !           120: object:2
        !           121: c_iter::next
        !           122: c_iter::valid = false
        !           123: ===foreach/rec===
        !           124: c::getIterator
        !           125: c_iter::__construct
        !           126: c_iter::valid = true
        !           127: c_iter::current
        !           128: c::getIterator
        !           129: c_iter::__construct
        !           130: c_iter::valid = true
        !           131: c_iter::current
        !           132: double:0:0
        !           133: c_iter::next
        !           134: c_iter::valid = true
        !           135: c_iter::current
        !           136: double:0:1
        !           137: c_iter::next
        !           138: c_iter::valid = true
        !           139: c_iter::current
        !           140: double:0:2
        !           141: c_iter::next
        !           142: c_iter::valid = false
        !           143: c_iter::next
        !           144: c_iter::valid = true
        !           145: c_iter::current
        !           146: c::getIterator
        !           147: c_iter::__construct
        !           148: c_iter::valid = true
        !           149: c_iter::current
        !           150: double:1:0
        !           151: c_iter::next
        !           152: c_iter::valid = true
        !           153: c_iter::current
        !           154: double:1:1
        !           155: c_iter::next
        !           156: c_iter::valid = true
        !           157: c_iter::current
        !           158: double:1:2
        !           159: c_iter::next
        !           160: c_iter::valid = false
        !           161: c_iter::next
        !           162: c_iter::valid = true
        !           163: c_iter::current
        !           164: c::getIterator
        !           165: c_iter::__construct
        !           166: c_iter::valid = true
        !           167: c_iter::current
        !           168: double:2:0
        !           169: c_iter::next
        !           170: c_iter::valid = true
        !           171: c_iter::current
        !           172: double:2:1
        !           173: c_iter::next
        !           174: c_iter::valid = true
        !           175: c_iter::current
        !           176: double:2:2
        !           177: c_iter::next
        !           178: c_iter::valid = false
        !           179: c_iter::next
        !           180: c_iter::valid = false
        !           181: ===foreach/key===
        !           182: c::getIterator
        !           183: c_iter::__construct
        !           184: c_iter::valid = true
        !           185: c_iter::current
        !           186: c_iter::key
        !           187: object:1st=>0
        !           188: c_iter::next
        !           189: c_iter::valid = true
        !           190: c_iter::current
        !           191: c_iter::key
        !           192: object:2nd=>1
        !           193: c_iter::next
        !           194: c_iter::valid = true
        !           195: c_iter::current
        !           196: c_iter::key
        !           197: object:3rd=>2
        !           198: c_iter::next
        !           199: c_iter::valid = false
        !           200: Done

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>