Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7926897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:01:32+00:00 2026-06-03T19:01:32+00:00

I have a problem that has been spoiling the way I want to do

  • 0

I have a problem that has been spoiling the way I want to do the things for a long time. It’s related to the use of magic get and set in PHP and trying to do a pre-increment over an object. I have a PHP class like the following:

class Foo {
    public $object;

    function __construct() {
            $this->object = array("bar" => 1);
    }

    function & __get($name) {
            return $this->object[$name];
    }

    function __set($name, $value) {
            echo "Old value: ". $this->object[$name] ." - New value: ". $value ."\n";
            $this->object[$name] = $value;
    }
}

Note the & in the __get method. Now I run this code:

$o = new Foo();

echo "First test\n";
$o->bar = 2;

echo "Second test\n";
$o->bar = $o->bar + 1;

echo "Third test\n";
$o->bar += 1;

echo "Fourth test\n";
++$o->bar;

And the output is:

First test
Old value: 1 - New value: 2
Second test
Old value: 2 - New value: 3
Third test
Old value: 4 - New value: 4
Fourth test
Old value: 5 - New value: 5

The third and fourth tests have an unexpected (by me) behaviour. Looks like $this->object[‘bar’] returns the value to be set, instead the old value as I expected. How come the value is already set even before it’s actually set?

If I remove the & from the __get method, this will work, so I guess it has something to do with the references management that PHP does. But I would expect the third test to behave the same way as the second, but it doesn’t.

I really don’t understand this. Any help would be much appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T19:01:47+00:00Added an answer on June 3, 2026 at 7:01 pm

    The results are in fact (after careful consideration) what I would expect.


    First test

    Code: $o->bar = 2;
    Output: Old value: 1 - New value: 2

    The operations, in order:

    • Assign a new value to $bar (call __set())

    Obviously this simply abandons the old value and puts a new value in its place. Nothing complicated.


    Second test

    Code: $o->bar = $o->bar + 1;
    Output: Old value: 2 - New value: 3

    The operations, in order:

    • Get a copy of $bar (call __get())
    • Add one to it
    • Assign a new value to $bar (call __set())

    The right side of the expression is evaluated, and the result is assigned to the left side. The value of the instance variable $bar is untouched during the evaluation of the right side, so you see it’s old value at the beginning of the __set() call.


    Third test

    Code: $o->bar += 1;
    Output: Old value: 4 - New value: 4

    The operations, in order:

    • Get a reference to $bar (call __get())
    • Add one to it
    • Assign a new value to $bar (call __set())

    This is interesting – at first glance I’m a little surprised that you see the output generated by __set() with this operation. Because you are incrementing the value held in the original zval, not assigning a whole new value – and therefore a whole new zval – as you were with the first two, it seems that the __set() call is redundant.

    However, when __set() is called the output that you do see is what you would expect, because the referenced value has been incremented before __set() was called. The value passed to __set() is the result of the increment operation, so it does not result in another one being added to it, it simply assigns the value of $foo->bar to $foo->bar – so you see the old and new values as being the same.


    Fourth test

    Code: ++$o->bar;
    Output: Old value: 5 - New value: 5

    …semantically identical to the third test. The exact same operations in the exact same order resulting in the exact same output. Again, a little odd that there is any output at all, but there you go.


    Fifth test that I invented

    Code: $o->bar++;
    Output: Old value: 5 - New value: 6

    …semantically identical to the second test. This is again interesting and slightly unexpected, but it also makes sense, given that $i++ returns the old value of $i.


    At a guess the reason that __set() is being called unnecessarily in the third and fourth tests is:

    • (unlikely but possible) because it would be more computationally expensive to work out whether __get() returned a reference or a copy than it is to simply call __set() – this seems unlikely since function calls are generally quite expensive.
    • (more likely) because the __set() function can contain code that is nothing to do with the actual assignment operation, and it would be potentially seriously inconvenient if this code did not run when the value was altered. For example, if one was to implement some kind of event driven architecture that fired events when certain values were altered, it would be annoying if that only worked with straight assignments.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that has been nagging me for some time now and
I have a really silly problem that has cost me a load of time
I have a problem that has been stumping me for a while, I wrote
I have a problem that has been bugging me all day. In my code
I have a problem that has been consuming me for days. I have a
I have a problem that is and has been perplexing me for hours. I
I have a problem that has been a thorn in my side for nearly
I have a reoccurring DNS problem that has been plaguing our users, occasionally causing
I have a problem that has been effectively reduced to a Travelling Salesman Problem
I have a problem concerning good application design that has been keeping me up

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.