php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79291 Clone provokes a recursive reference
Submitted: 2020-02-20 12:47 UTC Modified: 2020-02-20 12:57 UTC
From: ihorshein at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.2.28 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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ihorshein at gmail dot com
New email:
PHP Version: OS:

 

 [2020-02-20 12:47 UTC] ihorshein at gmail dot com
Description:
------------
- The result of clone passed as an argument provokes a recursive reference.
+ The result of clone stored in the variable that is passed as an argument does not cause problems.

Test script:
---------------
function assign(&$r, $c){
  $r = $c; 
}

class A{
  private $c;
  public function run(){
    assign($this->c, clone $this); // this code generates actual result
    
    //$c = clone $this;
    //assign($this->c, $c); this code generates the expected result
  }
}

$o = new A();
$o->run();
var_dump($o);

Expected result:
----------------
object(A)#27 (1) {
  ["c":"A":private]=>
  object(A)#13 (1) {
    ["c":"A":private]=>
    NULL
  }
}

Actual result:
--------------
object(A)#21 (1) {
  ["c":"A":private]=>
  &object(A)#20 (1) {
    ["c":"A":private]=>
    *RECURSION*
  }
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-02-20 12:57 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2020-02-20 12:57 UTC] nikic@php.net
The first example creates the reference before performing the clone, the second one creates the reference after the clone. If the clone is created afterwards, the reference is also preserved. Thus the difference in behavior.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 02:01:35 2024 UTC