xref: /PHP-5.4/tests/classes/__set__get_001.phpt (revision 26a0abbd)
1--TEST--
2ZE2 __set() and __get()
3--SKIPIF--
4<?php if (version_compare(zend_version(), '2.0.0-dev', '<')) die('skip ZendEngine 2 needed'); ?>
5--FILE--
6<?php
7class setter {
8	public $n;
9	public $x = array('a' => 1, 'b' => 2, 'c' => 3);
10
11	function __get($nm) {
12		echo "Getting [$nm]\n";
13
14		if (isset($this->x[$nm])) {
15			$r = $this->x[$nm];
16			echo "Returning: $r\n";
17			return $r;
18		}
19		else {
20			echo "Nothing!\n";
21		}
22	}
23
24	function __set($nm, $val) {
25		echo "Setting [$nm] to $val\n";
26
27		if (isset($this->x[$nm])) {
28			$this->x[$nm] = $val;
29			echo "OK!\n";
30		}
31		else {
32			echo "Not OK!\n";
33		}
34	}
35}
36
37$foo = new Setter();
38
39// this doesn't go through __set()... should it?
40$foo->n = 1;
41
42// the rest are fine...
43$foo->a = 100;
44$foo->a++;
45$foo->z++;
46var_dump($foo);
47
48?>
49--EXPECTF--
50Setting [a] to 100
51OK!
52Getting [a]
53Returning: 100
54Setting [a] to 101
55OK!
56Getting [z]
57Nothing!
58Setting [z] to 1
59Not OK!
60object(setter)#%d (2) {
61  ["n"]=>
62  int(1)
63  ["x"]=>
64  array(3) {
65    ["a"]=>
66    int(101)
67    ["b"]=>
68    int(2)
69    ["c"]=>
70    int(3)
71  }
72}
73