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
 [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

Pull Requests

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-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 04:00:01 2026 UTC