php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28050 Strange behaviour using references
Submitted: 2004-04-18 17:38 UTC Modified: 2004-06-30 03:59 UTC
Votes:5
Avg. Score:4.2 ± 0.7
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: mmertinkat at justseven dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.6 OS: Windows XP
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mmertinkat at justseven dot de
New email:
PHP Version: OS:

 

 [2004-04-18 17:38 UTC] mmertinkat at justseven dot de
Description:
------------
Just look at the code; it is reproducible with PHP 4.3.4, 4.3.5 and 4.3.6 on Windows and Linux.

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

    $data = NULL;

    $data['settings']['account']['firstname'] = 'Johnny';
    $data['settings']['account']['lastname'] = 'Walker';
    $data['settings']['account']['username'] = 'johnny';
    $data['settings']['account']['password'] = 'not secret';

    /**********************************************/

    print_r($data);

    $settings = $data['settings'];
    $account = &$settings['account'];

    // we're now going to change the account password and therefore
    // use the $account array as a "shortcut" (reference) to $settings['account']
    $account['password'] = 'secret';

    // write any changes into the data array
    $data['settings'] = $settings;


    /******************/

    // the following two lines will (mistakenly?) touch the $data array ...
    $mysettings = $data['settings'];
    $mysettings['account']['password'] = 'very secret';


    // ... whereas the next two lines don't change anything (as it should be)
    $myaccount = $data['settings']['account'];
    $myaccount['password'] = 'very very secret';

    print_r($data);

?>

Expected result:
----------------
Array
(
    [settings] => Array
        (
            [account] => Array
                (
                    [firstname] => Johnny
                    [lastname] => Walker
                    [username] => johnny
                    [password] => not secret
                )

        )

)

Array
(
    [settings] => Array
        (
            [account] => Array
                (
                    [firstname] => Johnny
                    [lastname] => Walker
                    [username] => johnny
                    [password] => secret
                )

        )

)


Actual result:
--------------
Array
(
    [settings] => Array
        (
            [account] => Array
                (
                    [firstname] => Johnny
                    [lastname] => Walker
                    [username] => johnny
                    [password] => not secret
                )

        )

)

Array
(
    [settings] => Array
        (
            [account] => Array
                (
                    [firstname] => Johnny
                    [lastname] => Walker
                    [username] => johnny
                    [password] => very secret
                )

        )

)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-29 23:00 UTC] sniper@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Please read the manual about references (what they are, what they are not, when to use, when not to use..)

 [2004-04-30 03:49 UTC] mmertinkat at justseven dot de
damn ... this is _definately_ a bug!

the problem is, that

$mysettings = $data['settings'];
$mysettings['account']['password'] = 'very secret';

should _NOT_ change anything within $data (because $data['settings'] is copied to $mysettings), but it does!
 [2004-05-06 09:09 UTC] bart at mediawave dot nl
Simplified version that has the same (wrong?) behavior:

<?php

$data['settings']['account']['password'] = 'not secret';

print_r($data);

$account =& $data['settings']['account'];
$mysettings = $data['settings'];
$mysettings['account']['password'] = 'very secret';

print_r($data);

?>

Pretty weird indeed.
 [2004-06-09 02:05 UTC] spy at spy dot zp dot ua
Indeed, it's very very strange behavior...
If even we consider that initial submission is not a bug, then this example - is a bug.

<?php

$data['settings']['account']['password'] = 'not secret';

$account =& $data['settings']['account'];
$mysettings = $data['settings'];

var_dump($mysettings); // here we can see that mysettings recognized as reference

unset($account); // MAGIC unset

var_dump($mysettings); // and now (!) it is not a reference

$mysettings['account']['password'] = 'very secret';

var_dump($data);


?>
 [2004-06-30 03:59 UTC] moriyoshi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

See bug #20993.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 18 04:01:29 2024 UTC