#19297 [NEW]: Php fails to update a globally defined reference's value
| From: | pkeshish at yahoo dot com | Date: | Mon, 09 Sep 2002 03:02:00 +0000 |
| Subject: | #19297 [NEW]: Php fails to update a globally defined reference's value | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-18699@lists.php.net to get a copy of this message | ||
From: pkeshish@yahoo.com
Operating system: Linux
PHP version: 4.2.2
PHP Bug Type: Scripting Engine problem
Bug description: Php fails to update a globally defined reference's value
<?php
// Dear Php developers/users,
//
// *-> Background
// I'm re-posting this bug (http://bugs.php.net/bug.php?id=17959)
// since the original one has been its status changed to
// 'Analyzed' and even after providing a new example that needs
// new analysis there are no means for me to change its status to
// 'Open' or anything else other than 'Closed'.
//
// I have sent email to hholzgra@php.net (person assigned to bug)
// asking him about it but never have heard back from him
// (neither by email nor update on the bug's comment page).
//
// Therefore, it seems that if I don't re-post this bug it will
// NEVER get any attention and possibly never get fixed.
//
// *-> Bug report
// Php fails to change/update a globally defined reference's
// value from within a user defined function using either of the
// following methods of accessing the reference variable:
//
// global $ptr; // where $ptr is the reference variable
// $GLOBALS['ptr']
//
// hholzgra@php.net explained (in original bug #17959) that I
// should not expect the first method to work as it is a "short
// hand" for "$ptr = &$GLOBALS['ptr'];" and a new assignment to
// local-to-function $ptr only changes the local reference and
// not the intended global one. Btw, I find this a counter
// intuitive behavior on the Php's part. I think "global $var"
// should act more like a C++ reference rather than a C pointer.
//
// After considering explanation of what exactly 'global $ptr'
// is i changed my code to strictly use $GLOBALS['ptr']. However,
// after doing so my program still didn't function properly!
//
// I had to work a bit harder to come up with a simplified example
// for you. I believe i have done so below.
//
// Please pay close attention to the notes w/in comments explaining
// strange conditions that would make this sample code to work as
// expected. Now I'm certain there EXISTS a serious problem with
// the Php engine when processing this code.
//
// Thanks,
// --patrick
//
// *-> Sample code
// define array we plan to navigate.
$top =
array
(
'name' => 'top',
'A' => array
(
'name' => 'A',
'A_a' => array
(
'name' => 'A_a',
'fn_1' => 'Aa_fn_1',
'fn_2' => 'Aa_fn_2',
'fn_3' => 'Aa_fn_3',
'A_a_1' => array
(
'name' => 'A_a_1',
'fn_1' => 'Aa1_fn_1',
'fn_2' => 'Aa1_fn_2',
),
'A_a_2' => array
(
'name' => 'A_a_2',
'fn_1' => 'Aa2_fn_1',
'fn_2' => 'Aa2_fn_2',
),
),
),
);
// setup "parent references" for each of the nodes.
$top['parent'] = null;
$top['A']['parent'] = &$top;
$top['A']['A_a']['parent'] = &$top['A'];
$top['A']['A_a']['A_a_1']['parent'] =
&$top['A']['A_a'];
$top['A']['A_a']['A_a_2']['parent'] =
&$top['A']['A_a'];
//$top['A']['A_a']['A_a_1']['parent'] =
&$top['A']['A_a'];
//
// Note!
// Here if we uncomment the above line the sample code will
// magically WORK!
//
// Why does the order of initialization matter!? And will the
// code now break elsewhere?
// setup our reference to $top. $ptr is what should change
// on every call to change_ptr(name).
$ptr = &$top;
// change_ptr( name ) does the following w/some verbose
// messages to display the state of variables in question.
//
// if ( ptr[name] )
// ptr = ptr[name];
//
function change_ptr( $name )
{
printf( " change_ptr($name) " );
if ( $GLOBALS['ptr'][$name] ) {
// Setting up string for later printing.
$before_change =
sprintf( "before: \$GLOBALS['ptr']['name'] = %s",
$GLOBALS['ptr']['name'] );
$GLOBALS['ptr'] = &$GLOBALS['ptr'][$name];
$after_change =
sprintf( "after: \$GLOBALS['ptr']['name'] = %s",
$GLOBALS['ptr']['name'] );
printf( "<font
color=\"green\"><b>CHANGED</b></font>:\n" );
printf( " %s\n %s", $before_change, $after_change );
}
else
printf( "<font color=\"red\"><b>NO
CHANGE!</b></font>" );
printf( "<br>\n" );
}
?>
<pre>
<?
// All of the following calls should succeed and print
// 'CHANGED'. However, you will notice that the last call
// will fail to change $ptr to point to the 'parent' of
// $top['A']['A_a']['A_a_2'].
//
change_ptr('A');
change_ptr('A_a');
change_ptr('A_a_1');
change_ptr('parent');
change_ptr('A_a_2');
change_ptr('parent');
?>
</pre>
?>
--
Edit bug report at http://bugs.php.net/?id=19297&edit=1
--
Try a CVS snapshot: http://bugs.php.net/fix.php?id=19297&r=trysnapshot
Fixed in CVS: http://bugs.php.net/fix.php?id=19297&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=19297&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=19297&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=19297&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=19297&r=support
Expected behavior: http://bugs.php.net/fix.php?id=19297&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=19297&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=19297&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=19297&r=globals