# Chaining __set and __get in an expression

- Thread: https://externals.io/message/11363
- Original archive: https://news-web.php.net/php.internals/11363
- Started by Curt Zirzow on 2004-07-20T04:26:46+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

---

## #11363 — Curt Zirzow — 2004-07-20T04:26:46+00:00

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

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!

---

## #11374 — Jason Garber — 2004-07-20T15:50:21+00:00

URL: https://externals.io/message/11363#11374 · In reply to: #11363

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!
