php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19297 Php fails to update a globally defined reference's value
Submitted: 2002-09-08 22:02 UTC Modified: 2002-09-09 05:10 UTC
From: pkeshish at yahoo dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.2 OS: Linux
Private report: No CVE-ID: None
 [2002-09-08 22:02 UTC] pkeshish at yahoo dot com
<?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>
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-09 05:10 UTC] sniper@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. Because of this, we hope you add your comments
to the original bug instead.

Thank you for your interest in PHP.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 06:01:34 2024 UTC