开发者

PHP caching gone wrong? Element used as reference, I can't see why

Something wicked has happened. I've lost two hours boiling a problem do开发者_如何学运维wn to this - two arrays of objects behaved as if one was a reference to the other.

foreach ($this->screenElements as $element) {
    echo($this->screenElementsSearch['tasksSeverity']->getValue());
    echo "-";

    $element->setValue('1cosmetical');

    echo($this->screenElementsSearch['tasksSeverity']->getValue());
    echo "\n";
}

PrintOut:

 - 1cosmetical
1cosmetical - 1cosmetical
1cosmetical - 1cosmetical

I neither set up $screenElementsSearch as a reference of $screenElements nor $element as a reference of $screenElements in the for loop.

Then, all of a sudden (I called phpinfo() to look up the PHP version to post this, 5.2.9 on Win7/Xampp lite by the way), it stopped happening. I know how this sounds. But printout changed to:

 - 
 - 
 -

like it ought to. Now, my question to you is: Has PHP or XAMPP some caching going on that may have caused this behaviour? Just in case it happens again, because then I'll bang my head against a wall.

Thank you very much in advance.


They don't need to be references. In PHP (like most other languages) objects have reference semantics. That is to say that assigning an object to two different variables will not clone the object. This is unlike scalar values and arrays.

In old versions of php (php 4), objects actually had value semantics. That is why php 4 code had ampersands all over the place.


Per your comment:

$this->screenElementsSearch = readEntityFields();
$this->screenElements = array_merge(someMethod(), $this->screenElementsSearch);

You are creating screenElements based on the contents of screenElementsSearch. In PHP 5, this actually copies references to the element objects from one array to the other. So both arrays hold their own references to the same objects, without you having to use &.

If the output is blank after you called phpinfo(), you probably triggered a switch somewhere that changed PHP versions on your server... that function by itself shouldn't really change anything though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜