xref: /PHP-8.3/ext/spl/tests/array_013.phpt (revision f8d79582)
1--TEST--
2SPL: ArrayIterator::append
3--FILE--
4<?php
5
6if (!class_exists('NoRewindIterator', false))
7{
8    require_once(__DIR__ . '/../examples/norewinditerator.inc');
9}
10
11echo "===Array===\n";
12
13$a = array(0 => 'zero', 1 => 'one', 2 => 'two');
14$it = new ArrayIterator($a);
15
16foreach($it as $key => $val)
17{
18    echo "$key=>$val\n";
19}
20
21echo "===Append===\n";
22
23$it->append('three');
24$it->append('four');
25
26foreach(new NoRewindIterator($it) as $key => $val)
27{
28    echo "$key=>$val\n";
29}
30
31echo "===Object===\n";
32
33class test
34{
35    public $zero = 0;
36    protected $pro;
37    public $one = 1;
38    private $pri;
39    public $two = 2;
40}
41
42$o = new test;
43$it = new ArrayIterator($o);
44
45foreach($it as $key => $val)
46{
47    echo "$key=>$val\n";
48}
49
50echo "===Append===\n";
51
52$it->append('three');
53$it->append('four');
54
55foreach(new NoRewindIterator($it) as $key => $val)
56{
57    echo "$key=>$val\n";
58}
59
60var_dump($o->{0}); /* doesn't wotk anyway */
61
62?>
63===DONE===
64--EXPECTF--
65===Array===
660=>zero
671=>one
682=>two
69===Append===
703=>three
714=>four
72===Object===
73zero=>0
74one=>1
75two=>2
76===Append===
77
78Fatal error: Uncaught Error: Cannot append properties to objects, use ArrayIterator::offsetSet() instead in %s:%d
79Stack trace:
80#0 %s(%d): ArrayIterator->append('three')
81#1 {main}
82  thrown in %s on line %d
83