php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #39202 Temporary tables with references dereference
Submitted: 2006-10-19 20:56 UTC Modified: 2006-10-20 18:12 UTC
From: cpriest at warpmail dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.1.6 OS: Apache 2.0.55
Private report: No CVE-ID: None
 [2006-10-19 20:56 UTC] cpriest at warpmail dot net
Description:
------------
References in temporary tables de-reference their references upon crossing function boundaries

Reproduce code:
---------------
<?php
  class myClassA {
	  public $Name = '';
	  function __construct($Name) { $this->Name = $Name;  }
  }
  
  global $tblData;
  
  $tblData = array(
	'Test'	=>	new myClassA('Object 1'),
	'Test2'	=>	new myClassA('Object 2'),
  );
  
  function getReferenced() {
	  global $tblData;
	  
	  $tblData2 = array(
		'Test'	=>	&$tblData['Test'],
		'Test2'	=>	&$tblData['Test2'],
	  );
	  return $tblData2;
  }
  
  echo '<plaintext>';
  
  print_r($tblData);
  
  $tblData2 = getReferenced();
  $tblData2['Test']->Name = 'Object 3';
  print_r($tblData2);
?>

Expected result:
----------------
I expect there to be only two objects at the end of the call, one with a Name of 'Object 3' and one with a Name of 'Object 2'

Actual result:
--------------
Three objects exist, 'Object 1', 'Object 2', and 'Object 3'

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-10-19 21:11 UTC] tony2001@php.net
Can't reproduce.
After the first print_r() I see "Object 1" (because the object haven't been changed yet), after ther second call I get "Object 3", which is expected too.

Hint: when we say "expected and actual results" we usually mean "copy/paste what you see and also attach what you expected to see", not a "I expect to see ..".
 [2006-10-20 00:18 UTC] cpriest at warpmail dot net
Expected:
------------------------------------------
Array
(
    [Test] => myClassA Object
        (
            [Name] => Object 3
        )

    [Test2] => myClassA Object
        (
            [Name] => Object 2
        )

)
Array
(
    [Test] => myClassA Object
        (
            [Name] => Object 3
        )

    [Test2] => myClassA Object
        (
            [Name] => Object 2
        )

)


Actual:
--------------------------------------------
Array
(
    [Test] => myClassA Object
        (
            [Name] => Object 1
        )

    [Test2] => myClassA Object
        (
            [Name] => Object 2
        )

)
Array
(
    [Test] => myClassA Object
        (
            [Name] => Object 3
        )

    [Test2] => myClassA Object
        (
            [Name] => Object 2
        )

)
 [2006-10-20 08:42 UTC] tony2001@php.net
See my explanation in the previous message.
No bug here.
 [2006-10-20 16:07 UTC] cpriest at warpmail dot net
I think there's some confusion here, if you look at the code source I am only creating two objects.  When I call getReferenced() it returns a table which has references to the created objects.

When I go to change the name of the object in the second table (the one with references) it doesn't change the real object.  For some reason there are then three objects when only two have been created.

If this is the expected behavior can you explain why?  I don't understand why a third object is being created when I have used references.
 [2006-10-20 16:15 UTC] tony2001@php.net
>it doesn't change the real object.
It does, but you call print_r() BEFORE changing it =)
 [2006-10-20 17:04 UTC] cpriest at warpmail dot net
> It does, but you call print_r() BEFORE changing it =)

I'm calling print_r() twice, once before and once after.  The before shows correctly the after shows three objects exist...  What am I missing?
 [2006-10-20 17:15 UTC] tony2001@php.net
Where did you manage to find THREE objects here?

Array
(
    [Test] => myClassA Object
        (
            [Name] => Object 3
        )

    [Test2] => myClassA Object
        (
            [Name] => Object 2
        )

)
 [2006-10-20 17:48 UTC] cpriest at warpmail dot net
Okay you're right, I'm an idiot, I see my error.

I saw something very similar occur in more complicated code and I tried to write a simpler test case for it.

This obviously isn't right and doesn't demonstrate properly (nor even demonstrate an issue).  I'll come up with something better, sorry Tony!
 [2006-10-20 18:12 UTC] cpriest at warpmail dot net
I cannot reproduce, I'll give up.  Sorry to waste your time Tony.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 23:01:27 2024 UTC