# __get() and __set() in PHP 5

- Thread: https://externals.io/message/3393
- Original archive: https://news-web.php.net/php.internals/3393
- Started by Paul Hudson on 2003-07-12T19:03:38+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3393 — Paul Hudson — 2003-07-12T19:03:38+00:00

URL: https://externals.io/message/3393#3393

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

---

## #3394 — Andi Gutmans — 2003-07-12T20:41:12+00:00

URL: https://externals.io/message/3393#3394 · In reply to: #3393

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
