__get() and __set() in PHP 5

From: Date: Sat, 12 Jul 2003 19:03:38 +0000
Subject: __get() and __set() in PHP 5
Groups: php.internals 
Request: Send a blank email to internals+get-3393@lists.php.net to get a copy of this message
All, I've been poking around with these two functions while testing the build from CVS, and am not sure how they're supposed to work. From what I can gather, they are only called when changing a variable that wasn't in the class definition. I say that because I took the example script from http://uk.php.net/zend-engine-2.php and stripped it down to this: <?php class Setter { public $n; public $z; function __get($nm) { print "Getting [$nm]\n"; } function __set($nm, $val) { print "Setting [$nm] to $val\n"; } } $foo = new Setter(); $foo->n = 1; $foo->z++; $foo->z++; ?> That script outputs nothing, despite reading and writing to z twice, and writing to n once. If you comment out the "public $n;" and "public $z;" lines, you get the following output: Setting [n] to 1 Getting [z] Setting [z] to 1 Getting [z] Setting [z] to 1 ... which is what I was expecting. Are __get() and __set(), then, only supposed to work with non-declared variables, or am I doing something wrong? Thanks, --Paul

« previous php.internals (#3393) next »