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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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 Jun 02 08:01:31 2024 UTC