xref: /PHP-8.1/tests/classes/__set__get_005.phpt (revision f8d79582)
1--TEST--
2ZE2 __set() and __get()
3--FILE--
4<?php
5class Test
6{
7    protected $x;
8
9    function __get($name) {
10        echo __METHOD__ . "\n";
11        if (isset($this->x[$name])) {
12            return $this->x[$name];
13        }
14        else
15        {
16            return NULL;
17        }
18    }
19
20    function __set($name, $val) {
21        echo __METHOD__ . "\n";
22        $this->x[$name] = $val;
23    }
24}
25
26class AutoGen
27{
28    protected $x;
29
30    function __get($name) {
31        echo __METHOD__ . "\n";
32        if (!isset($this->x[$name])) {
33            $this->x[$name] = new Test();
34        }
35        return $this->x[$name];
36    }
37
38    function __set($name, $val) {
39        echo __METHOD__ . "\n";
40        $this->x[$name] = $val;
41    }
42}
43
44$foo = new AutoGen();
45$foo->bar->baz = "Check";
46
47var_dump($foo->bar);
48var_dump($foo->bar->baz);
49
50?>
51--EXPECTF--
52AutoGen::__get
53Test::__set
54AutoGen::__get
55object(Test)#%d (1) {
56  ["x":protected]=>
57  array(1) {
58    ["baz"]=>
59    string(5) "Check"
60  }
61}
62AutoGen::__get
63Test::__get
64string(5) "Check"
65