php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75632 Wrong behavior of clone and references
Submitted: 2017-12-05 14:08 UTC Modified: 2017-12-06 08:35 UTC
From: kripper at imatronix dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.12 OS: All
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: kripper at imatronix dot com
New email:
PHP Version: OS:

 

 [2017-12-05 14:08 UTC] kripper at imatronix dot com
Description:
------------
Setting a unused reference to a cloned object property, prevents the object to be cloned.
This makes no sense and is intuitive.

Test script:
---------------
$orig = new stdClass();
$orig->test = 'original';

$ptr = & $orig->test; // <--- Setting an unused reference changes the behaviour 

$copy = clone $orig;
$copy->test = "modified";

die("Orig is: {$orig->test}"); // Gives 'modified' instead of 'original'

Expected result:
----------------
$orig->test should give 'original' since we cloned the object.
A cloned object should be a cloned object, no matter of unused references.


Actual result:
--------------
Setting a reference makes "$copy = clone $original" to behave just like "$copy = $original" (a reference).

Not setting or unsetting the refernce, fixes the behavior.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-05 14:55 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2017-12-05 14:55 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

References are not pointers: $ptr does not "point" to $orig->test, rather they both use the same underlying value stored in memory. Also, creating a reference forces both sides of the assignment to be references - otherwise the system wouldn't work.
http://php.net/manual/en/language.references.php

If you unset($ptr) before the clone then $orig->test will become the only reference to its value left and so automatically revert to a normal unreferenced value.
https://3v4l.org/c6epk
 [2017-12-05 18:51 UTC] kripper at imatronix dot com
<?php

// We used to have objects with multilevel array data...
$base = new stdClass();
$base->data = array(
	'nodes' => array(
		'subNodes' => array(
			'subSubNodes' => '...'
		)
	)
);

// And we used references to write cleaner code:
$ref = & $base->data['nodes']['subNodes']['subSubNodes'];

// ChangeData($ref);


// Instead of:

// ChangeData($base->data['nodes']['subNodes']);

// This has a very big impact on legibility, if not performance.

// But since PHP 7.1, keeping references prevents 'clone' to work properly
// because the cloned object properties are treated as references to the
// original object properties.

$clone = clone $base;

// So, when changing the cloned object...
$clone->data['nodes']['subNodes'] = 'Changed by clone';

// ...the original object is also modified (result is: 'Changed by clone').
die("Base object has property: " . $base->data['nodes']['subNodes']);

It seems like references are not really references anymore, but variable aliases, which cannot be usted together with clone().
Actually, we feel references aren't safe nor usefull anymore, but for 'out arguments' when calling functions (which can better be done by returning arrays anyway).
Of course, we can unset the reference before doing clone, but it's not feasible cosidering we can accidently call clone from a subfunction while the reference is still alive.

Is there any solution or workaround?

If not, we probably have a language design problem here that will make noise to serious developers until PHP introduces pointers or references that can be usted together with clone.
 [2017-12-05 19:13 UTC] requinix@php.net
> // But since PHP 7.1, ...

> It seems like references are not really references anymore,
They've always worked that way. https://3v4l.org/70ZIS

> Of course, we can unset the reference before doing clone, but it's not feasible
> cosidering we can accidently call clone from a subfunction while the reference is
> still alive.

> Is there any solution or workaround?
Unsetting the reference before the function call is the answer. Not using a reference in the first place is an even better answer.

Using a reference for legibility? I don't see how
  ChangeData($ref);
is more understandable than
  ChangeData($base->data['nodes']['subNodes']);

Using a reference for performance? Don't. That's the #1 way to abuse references. PHP is already efficient with variables and it already has copy-on-write functionality so don't fight it by trying to be clever.


If you want to propose additions like pointers or changes like... I don't know what you want to change with references... then check out PHP's RFC process.
https://wiki.php.net/rfc/howto
 [2017-12-05 21:39 UTC] kripper at imatronix dot com
Thanks Damian,

I want to write legible code like this:

$ref = & $base->data['nodes']['subNodes']['subSubNodes'];
foreach($res as &val) {
	if(!$res['sub']) $res['sub'] = 'something';
	CloneObjectsWithoutUnsettingRefs($res);
	if($res['other'] = 'ok') $res['ok'] = true;
}

Instead of:

foreach($base->data['nodes']['subNodes']['subSubNodes'] as &val) {
	if(!$base->data['nodes']['subNodes']['subSubNodes']['sub']) $base->data['nodes']['subNodes']['subSubNodes']['sub'] = 'something';
	CloneObjectsWithoutUnsettingRefs($base->data['nodes']['subNodes']['subSubNodes']);
	if($base->data['nodes']['subNodes']['subSubNodes']['other'] = 'ok') $base->data['nodes']['subNodes']['subSubNodes']['ok'] = true;
}

What is the best approach for this?
Using references causes problems when cloning objects.
 [2017-12-06 08:35 UTC] requinix@php.net
That code is a bit weird so I'm going to make what I think are some corrections:

  $ref = $base->data['nodes']['subNodes']['subSubNodes'];
  foreach($ref as $key => $res) {
    if(!$res['sub']) $res['sub'] = 'something';
    $res = CloneObjectsWithoutUnsettingRefs($res);
    if($res['other'] == 'ok') $res['ok'] = true;
    $ref[$key] = $res;
  }
  $base->data['nodes']['subNodes']['subSubNodes'] = $ref;

Point is you can modify the copies and then overwrite the originals.
 [2018-02-10 20:09 UTC] kripper at imatronix dot com
Thanks for your time.
I'm afraid that the posted code is not a solution for the general case.
My previous comments were not quite clear so I wrote something more elaborated here:

https://docs.google.com/document/d/11_uIZyvXzPBTv8-1id-xl9itYpJ4lGaSquWhEvmPgK4/edit#heading=h.lbjyy98sok62
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 06:01:29 2024 UTC