[PHP-BUG] Bug #77234 [NEW]: clone keyword not working as spected

[email protected] ("genialjacob at gmail dot com") Mon, 03 Dec 2018 14:38:48 +0000
Newsgroups php.standards
Message-ID <[email protected]>
From:             genialjacob at gmail dot com
Operating system: Windows 10
PHP version:      7.2.12
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:clone keyword not working as spected

Description:
------------
I have a method (addFinal) in a class that copy an object from an array
and put it into another, I use clone to avoid the possibility to modify
the original object. 

The modification "ONLY_MODIFY_THIS" affects several objects, when is
supposed to affect only the first.

I want two things:

1. Preserve original objects D1, D2 and D3

2. Modify only the object indexed in the finals array.

Test script:
---------------
class I {
  public $name;
  public $value;

  public function __construct( $name, $value )
  {
    $this->name = $name;
    $this->value = $value;
  }
}

class D {
  public $name;
  public $items;
  public function __construct( $name )
  {
    $this->name = $name;
    $this->items = array(
      'I1' => new I( "I1", 1232 ),
      'I2' => new I( "I2", 12 ),
      'I3' => new I( "I3", "hello" )
    );
  }

  public function addItem( Item $item )
  {
    array_push( $this->items, $item );
  }
}

class B {
  public $values; 
  public function __construct()
  {
    $this->values = array(
      "D1" => new D("D1"), 
      "D2" => new D("D2"), 
      "D3" => new D("D3"));
  }   
}

class A {
  public $name;
  public $bobject;
  public $finals;  
  public function __construct()
  {
    $this->bobject = new B();
    $this->finals = array();
  }
  public function addFinal( $name )
  {
    $final = clone $this->bobject->values[$name];
    array_push( $this->finals, $final );
  }
}

class C extends A {

}


$c = new C();
$c->addFinal( "D1" );
$c->addFinal( "D1" );
$c->addFinal( "D1" );
$c->addFinal( "D1" );
$c->addFinal( "D2" );
$c->addFinal( "D3" );
$c->addFinal( "D1" );

$c->finals[0]->name = "HelloWorld";
$c->finals[0]->items["I1"]->name="ONLY_MODIFY_THIS";

print_r( $c );
exit;

Expected result:
----------------
1. Preserve original objects D1, D2 and D3

2. Modify only the object indexed in the finals array.

Actual result:
--------------
The code is modifying multiple variable prevously cloned from an
original object. I believe something is wrong there.

-- 
Edit bug report at https://bugs.php.net/bug.php?id=77234&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=77234&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=77234&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=77234&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=77234&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=77234&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=77234&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=77234&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=77234&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=77234&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=77234&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=77234&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=77234&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=77234&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77234&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=77234&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=77234&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=77234&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77234&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=77234&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=77234&r=mysqlcfg