|  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: 2020-10-29 17:26 UTC
From: sdaicz at dc dot uba dot ar Assigned: cmb (profile)
Status: Closed 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.
 [2020-10-29 17:26 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2020-10-29 17:26 UTC]
Indeed, works again as of PHP 7.1.0[1], and PHP 7.0 is EOL.

[1] <>
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jan 23 02:01:25 2021 UTC