php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75629 Object property references are replaced by references created in functions
Submitted: 2017-12-05 09:21 UTC Modified: 2018-07-15 19:31 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: ocramius@php.net Assigned:
Status: Feedback Package: Scripting Engine problem
PHP Version: Irrelevant OS: Linux x64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ocramius@php.net
New email:
PHP Version: OS:

 

 [2017-12-05 09:21 UTC] ocramius@php.net
Description:
------------
See https://3v4l.org/BNDsu

The PHP engine seems to globally replace references of object properties when new  object property references are created in a by-ref function call.

The problem occurs when defining a by-ref parameter in a function and then calling it with an object property:

function foo(& $param) {
    // keep a non-gc reference to $param here and things will break
}

The attached test case demonstrates the issue by using `clone` and an array of references. The same example works when the references are kept in another object.

Possible causes here are:

 * clone is misbehaving
 * references are misbehaving overall (YIKES!)
 * references created in functions are cached somewhere

Test script:
---------------
--TEST--
Keeping a reference to an object property in an array should not override existing references
?>
--FILE--
<?php

class Container
{
    /** @var \stdClass|null */
    public $referencedProperty;

    public function __clone()
    {
        $this->referencedProperty = clone $this->referencedProperty;
    }
}

$references = [];

$container = new Container();

$loadFieldByReference = function (& $referencedProperty) use (& $references) {
    $referencedProperty = new \stdClass();
    $references[]       = & $referencedProperty;
};

$loadFieldByReference($container->referencedProperty);

$container->referencedProperty->publicProperty = 123;
$clone                                         = clone $container;
$clone->referencedProperty->publicProperty     = 234;

echo $container->referencedProperty->publicProperty, "\n";
echo $clone->referencedProperty->publicProperty, "\n";
echo $container->referencedProperty === $clone->referencedProperty ? "same\n" : "not same\n";

?>
--EXPECT--
123
234
not same



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-05 09:24 UTC] ocramius@php.net
Causes https://github.com/Ocramius/ProxyManager/pull/396 (original source of the bug)
 [2017-12-05 11:04 UTC] nikic@php.net
-Status: Open +Status: Feedback
 [2017-12-05 11:04 UTC] nikic@php.net
Try clarify, is the behavior you object to the fact that PHP preserves references while cloning? https://3v4l.org/c4iov
 [2017-12-05 12:00 UTC] ocramius@php.net
@nikic two incorrect things are going on here:

 1. the `referencedProperty` is cloned too here (via `__clone`), so it should be a new instance anyway
 2. the `Container#$referencedProperty` is never a reference itself, but it is passed by reference to the `$loadFieldByReference()` closure and stored in `$references`
 [2017-12-05 15:28 UTC] ocramius@php.net
As discussed with Nikic, this issue is likely a misunderstanding in how references work. References are bi-directional, and have to be maintained until necessary. 

In the test case above, the reference becomes unnecessary as soon as the closure execution ends, but it is to be kept when pushing it to an array.

This specific line is actually acting on a reference, and writing a clone to a reference, therefore overwriting both objects:

`$this->referencedProperty = clone $this->referencedProperty`

A solution here is to de-reference the value by moving the reference:

```
$ref = & $this->referencedProperty;
unset($this->referencedProperty);
$this->referencedProperty = clone $ref;
```

I'm still not convinced that this is right, as it basically makes references a complete nightmare to work it (not that it was a pleasure before this).
 [2018-06-24 04:25 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 [2018-07-15 19:31 UTC] ocramius@php.net
-Status: No Feedback +Status: Feedback
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Oct 22 08:01:25 2018 UTC