skip to Main Content

I’m trying to change a global value that is currently a reference to another value. My current implementation doesn’t work:

$anna = array('Name' => "Anna");
$bella = array('Name' => "Bella");
$girl = &$anna;

function change(){
    global $girl, $bella;
    $girl = &$bella;
    // $girl['Name'] is now 'Bella'
    output_girl(); // This still outputs 'Anna'
}

function output_girl(){
    global $girl;
    echo $girl['Name'];
}

change();

// What do we want to see?
echo $anna['Name']; // Should still be 'Anna'
echo $bella['Name']; // Should still be 'Bella'
echo $girl['Name']; // Should now be 'Bella' (in above example this will still be 'Anna')

Important notes:

  • A workaround with a return value like $girl = &change(); is not a solution to my problem. It’s important that the global $girl is changed before the end of the change() function. (Reason being this is a problem inside a complex project with nested function calls all wanting to use the same global variables.)
  • I am aware a workaround could be coded using the $GLOBALS array. However, I am specifically interested if this problem can be solved using the global keyword as seen in the example code. And if not, to know why this isn’t possible in PHP.
  • Removing the &to make it $girl = $bella is also not a solution. The global $girl object needs to always be a reference to the original arrays defined at the start of the script. We cannot ever make copies / break references.

Question: Is this an impossibility in PHP when I use the global keyword? If so, can someone explain why? If not, can someone explain how to make this work?

2

Answers


  1. It’s impossible to do that in PHP with only global keyword.

    This is because references in php are not same as pointers as we know them for example from C/C++.

    When you do $girl = &$anna; on line 3 it doesn’t store address of $anna variable in $girl variable. Rather the new symbol $girl is created to reference same variable as symbol $anna.

    When you do global $girl, $bella; inside of a function. New local symbols $girl and $bella are created and they are made to reference their global counterpart.

    When you do $girl = &$bella; inside the function the local symbol $girl is changed to reference same variable as local symbol $bella but the global symbol $girl is not changed. After doing that both local symbols $girl and $bella are referencing same global symbol $bella.

    Login or Signup to reply.
  2. The problem is that your global is still referencing to $anna.

    you can check it by adding print_r($GLOBALS[‘girl’]):

    function change(){
        global $girl, $bella;
        $girl = &$bella;
        print_r($girl); //referencing to bella
        print_r($GLOBALS['girl']); //referencing to Anna
        output_girl(); // This still outputs 'Anna'
    }
    

    }

    I don’t know the correct solution for this, but there is a way around it. Just add:

    function change(){
        global $girl, $bella;
        $GLOBALS['girl'] = &$bella;
        output_girl(); // now is 'Bella'
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search