|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75692 Changing properties of cloned object can alter the original
Submitted: 2017-12-15 15:38 UTC Modified: -
From: sdaicz at dc dot uba dot ar Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 7.0.26 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.
Block user comment
Status: Assign to:
Bug Type:
From: sdaicz at dc dot uba dot ar
New email:
PHP Version: OS:


 [2017-12-15 15:38 UTC] sdaicz at dc dot uba dot ar
- Clone an object
- Iterate by reference through the attribute values of either the original or the clone
- Change attribute values inside the loop
- Both objects get their attribute values changed instead of only the one intended

Tested on Windows and Linux.
This seems to happen only in php 7.0.
php 7.1 and 5.6 both work as expected.

Test script:
$a = new \stdClass();
$a->b = 1;
$a->c = 2;
$d = clone $a;
foreach ($d as $key=>&$value) {
	$value += 10;

Expected result:
Object $a should keep its original values ("b"=>1, "c"=>2). This is what happens in 7.1 and 5.6:

class stdClass#1 (2) {
  public $b =>
  public $c =>
class stdClass#2 (2) {
  public $b =>
  public $c =>

Actual result:
Object $a is altered by the loop, resulting in the same values as $d

object(stdClass)#1 (2) {
object(stdClass)#2 (2) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2017-12-15 15:44 UTC]
> This seems to happen only in php 7.0.
> php 7.1 and 5.6 both work as expected.

The behaviour appears right not only in 7.1 but in 7.2.

PHP 7.0 had "Active Support Until 12 days ago" - also that behaviour is unlikely to be fixed in point version.
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Oct 28 23:01:24 2020 UTC