Chaining __set and __get in an expression

php.internals

Curt Zirzow

22 years ago
Given the class definition: <?php class Caller { private $x = array('a' => false); function __set($name, $value) { echo "setting\n"; $this->x[$name] = 'foo'; // intentially ignore value } function __get($name) { echo "getting\n"; return $this->x[$name]; } } $foo = new Caller(); $b = $foo->a = 'bar'; echo "b is " . $b . "\n"; /* output: setting b is bar */ ?> I sort of expected both __set and __get to be called. Is it concievable to have them both called? The other alternative If possible, is allowing a return value from __set() and using that value for the rest of the expression. Curt
-- First, let me assure you that this is not one of those shady pyramid schemes you've been hearing about. No, sir. Our model is the trapezoid!

Jason Garber

22 years ago
Hello Curt, From http://us2.php.net/manual/en/language.operators.assignment.php "The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3." I take this to mean that ($foo->a = 'bar') will always return 'bar' and that is a core feature of the language. Remember that $foo->a is the left hand operand, and is only receiving the value. It is the = operator that returns the value of the expression. It may also be worth noting that the Associativity of the `=` operator is "Right".
-- Best regards, Jason mailto:jason@ionzoft.com Tuesday, July 20, 2004, 12:26:46 AM, you wrote: CZ> Given the class definition: CZ> <?php CZ> class Caller { CZ> private $x = array('a' => false); CZ> function __set($name, $value) { CZ> echo "setting\n"; CZ> $this->x[$name] = 'foo'; // intentially ignore value CZ> } CZ> function __get($name) { CZ> echo "getting\n"; CZ> return $this->x[$name]; CZ> } CZ> } CZ> $foo = new Caller(); $b = $foo->>a = 'bar'; CZ> echo "b is " . $b . "\n"; CZ> /* output: CZ> setting CZ> b is bar CZ> */ ?>> CZ> I sort of expected both __set and __get to be called. Is it CZ> concievable to have them both called? CZ> The other alternative If possible, is allowing a return value CZ> from __set() and using that value for the rest of the expression. CZ> Curt CZ> -- CZ> First, let me assure you that this is not one of those shady pyramid schemes CZ> you've been hearing about. No, sir. Our model is the trapezoid!