xref: /PHP-7.4/tests/classes/__set__get_005.phpt (revision 782352c5)
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===DONE===
52--EXPECTF--
53AutoGen::__get
54Test::__set
55AutoGen::__get
56object(Test)#%d (1) {
57  ["x":protected]=>
58  array(1) {
59    ["baz"]=>
60    string(5) "Check"
61  }
62}
63AutoGen::__get
64Test::__get
65string(5) "Check"
66===DONE===
67