Re: __get() and __set() in PHP 5

From: Date: Sat, 12 Jul 2003 20:41:12 +0000
Subject: Re: __get() and __set() in PHP 5
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-3394@lists.php.net to get a copy of this message
Yes, you are right. They only work if the variable is not defined. Andi At 08:03 PM 12/7/2003 +0100, Paul Hudson wrote:
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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.internals (#3394) next »