xref: /PHP-7.3/Zend/tests/anon/005.phpt (revision 49608e06)
1--TEST--
2testing reusing anons that implement an interface
3--FILE--
4<?php
5class Outer {
6    protected $data;
7
8    public function __construct(&$data) {
9        /* array access will be implemented by the time we get to here */
10        $this->data = &$data;
11    }
12
13    public function getArrayAccess() {
14        /* create a child object implementing array access */
15        /* this grants you access to protected methods and members */
16        return new class($this->data) implements ArrayAccess {
17            public function offsetGet($offset) { return $this->data[$offset]; }
18            public function offsetSet($offset, $data) { return ($this->data[$offset] = $data); }
19            public function offsetUnset($offset) { unset($this->data[$offset]); }
20            public function offsetExists($offset) { return isset($this->data[$offset]); }
21        };
22    }
23}
24
25$data = array(
26    rand(1, 100),
27    rand(2, 200)
28);
29
30$outer = new Outer($data);
31$proxy = $outer->getArrayAccess();
32
33/* null because no inheritance, so no access to protected member */
34var_dump(@$outer->getArrayAccess()[0]);
35--EXPECT--
36NULL
37