xref: /PHP-8.4/ext/spl/tests/array_007.phpt (revision 902d6439)
1--TEST--
2SPL: ArrayObject/Iterator from IteratorAggregate
3--FILE--
4<?php
5
6// This test also needs to exclude the protected and private variables
7// since they cannot be accessed from the external object which iterates
8// them.
9
10#[AllowDynamicProperties]
11class test implements IteratorAggregate
12{
13    public    $pub = "public";
14    protected $pro = "protected";
15    private   $pri = "private";
16
17    function __construct()
18    {
19        $this->imp = "implicit";
20    }
21
22    function getIterator(): Traversable
23    {
24        $it = new ArrayObject($this);
25        return $it->getIterator();
26    }
27};
28
29$test = new test;
30$test->dyn = "dynamic";
31
32print_r($test);
33
34print_r($test->getIterator());
35
36foreach($test as $key => $val)
37{
38    echo "$key => $val\n";
39}
40
41?>
42--EXPECT--
43test Object
44(
45    [pub] => public
46    [pro:protected] => protected
47    [pri:test:private] => private
48    [imp] => implicit
49    [dyn] => dynamic
50)
51ArrayIterator Object
52(
53    [storage:ArrayIterator:private] => ArrayObject Object
54        (
55            [storage:ArrayObject:private] => test Object
56                (
57                    [pub] => public
58                    [pro:protected] => protected
59                    [pri:test:private] => private
60                    [imp] => implicit
61                    [dyn] => dynamic
62                )
63
64        )
65
66)
67pub => public
68imp => implicit
69dyn => dynamic
70