Bug #79377 [Opn->Nab]: Property is not cloned with clone

From: Date: Fri, 13 Mar 2020 00:59:13 +0000
Subject: Bug #79377 [Opn->Nab]: Property is not cloned with clone
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226082@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79377&edit=1 ID: 79377 Updated by: requinix@php.net Reported by: mvorisek at mvorisek dot cz Summary: Property is not cloned with clone -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: Any PHP Version: 7.4.3 Block user comment: N Private report: N New Comment: You made the property a reference. PHP quite happily cloned the reference. Previous Comments: ------------------------------------------------------------------------ [2020-03-13 00:57:32] mvorisek at mvorisek dot cz Description: ------------ Seems like a core bug presented in all PHP versions (5.4.0 - 7.4.3 tested). Please verify and fix asap. Test script: --------------- class A { public $elements = []; public function __construct() { $this->fields = &$this->elements; // this causes the issue } public function __clone() { // $elems = $this->elements; unset($this->elements); $this->elements = $elems; $this->fields = &$this->elements; // this is an immediate fix before fixed in PHP directly } } $x = new A(); $y = clone $x; $y->elements['hello'] = 'world'; print_r($x); print_r($y); Expected result: ---------------- A Object ( [elements] => Array ( ) [fields] => Array ( ) ) A Object ( [elements] => Array ( [hello] => world ) [fields] => Array ( [hello] => world ) ) Actual result: -------------- A Object ( [elements] => Array ( [hello] => world ) [fields] => Array ( [hello] => world ) ) A Object ( [elements] => Array ( [hello] => world ) [fields] => Array ( [hello] => world ) ) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79377&edit=1

« previous php.bugs (#226082) next »