Bug #77234 [Opn->Nab]: clone keyword not working as spected
| From: | cmb@php.net | Date: | Mon, 03 Dec 2018 16:12:34 +0000 |
| Subject: | Bug #77234 [Opn->Nab]: clone keyword not working as spected | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-218257@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77234&edit=1
ID: 77234
Updated by: cmb@php.net
Reported by: genialjacob at gmail dot com
Summary: clone keyword not working as spected
-Status: Open
+Status: Not a bug
Type: Bug
-Package: PHP Language Specification
+Package: Scripting Engine problem
Operating System: Windows 10
PHP Version: 7.2.12
Block user comment: N
Private report: N
New Comment:
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
From the docs[1]:
| When an object is cloned, PHP 5 will perform a shallow copy of
| all of the object's properties.
This is the same for PHP 7 (will fix this minor issue right away).
You are looking for a deep copy.
[1] <http://php.net/manual/en/language.oop5.cloning.php>
Previous Comments:
------------------------------------------------------------------------
[2018-12-03 14:38:48] genialjacob at gmail dot com
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 this bug report at https://bugs.php.net/bug.php?id=77234&edit=1