php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49998 ArrayIterator does not clone itself when referencing itself
Submitted: 2009-10-26 13:44 UTC Modified: 2009-10-26 22:40 UTC
From: christopheb156 at hotmail dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.2SVN-2009-10-26 (snap) OS: windows xp
Private report: No CVE-ID: None
 [2009-10-26 13:44 UTC] christopheb156 at hotmail dot com
Description:
------------
In test file array_022.phpt we see the clone being changed but this does not change original, If you clone an ArrayIterator you also clone its reference.

If you take such a self-wrapping ArrayIterator and clone it, the clone is no longer self-wrapping: the clone wraps the original. HOWEVER, I *think* the SPL_ARRAY_IS_SELF flag is being copied to the clone 

Therefore, the clone starts to store/retrieve data from itself rather than from its wrapped container - hence the difference. 

Reproduce code:
---------------
<?php

class MyArrayIterator extends ArrayIterator
{
	public function __construct()
	{
		parent::__construct($this);
		$this['bar'] = 'baz';
	}
}

$a = new MyArrayIterator;

$b = clone $a;
$b['baz'] = 'Foo';

var_dump($a);
var_dump($b);

?>

Expected result:
----------------
object(MyArrayIterator)#%d (2) {
  ["bar"]=>
  string(3) "baz"
  ["baz"]=>
  string(3) "Foo"
}
object(MyArrayIterator)#%d (2) {
  ["bar"]=>
  string(3) "baz"
  ["baz"]=>
  string(3) "Foo"
}

Actual result:
--------------
object(MyArrayIterator)#%d (1) {
  ["bar"]=>
  string(3) "baz"
}
object(MyArrayIterator)#%d (2) {
  ["bar"]=>
  string(3) "baz"
  ["baz"]=>
  string(3) "Foo"
}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-10-26 22:40 UTC] jani@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 09:01:28 2024 UTC