#28050 [Com]: Strange behaviour using references
| From: | bart at mediawave dot nl | Date: | Thu, 06 May 2004 07:09:14 +0000 |
| Subject: | #28050 [Com]: Strange behaviour using references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-59287@lists.php.net to get a copy of this message | ||
ID: 28050
Comment by: bart at mediawave dot nl
Reported By: mmertinkat at justseven dot de
Status: Open
Bug Type: Scripting Engine problem
Operating System: Windows XP
PHP Version: 4.3.6
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2004-04-30 03:49:04] 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-04-29 23:00:00] 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-18 17:38:04] 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
)
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28050&edit=1