Skip to content

Instantly share code, notes, and snippets.

@zither
Last active November 17, 2016 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zither/3cfbc08a1da07731e8c605146773f4cc to your computer and use it in GitHub Desktop.
Save zither/3cfbc08a1da07731e8c605146773f4cc to your computer and use it in GitHub Desktop.
reference rebinding test
<?php
$a = "111";
$b = &$a;
$f = "????";
function test_global_ref()
{
$c = "2222";
global $b; // 这个 $b 与之前函数外的 $b 不是同一个,位于不同的 scope, 它是对函数外那个 $b 引用,此时值为 "111"
$b = &$c; // 重新绑定后这个 $b 变成了 $c 的引用, 修改的只是函数 scope 内的 $b
var_dump($b); // 此时值为 "2222", 但是并未改变函数 scope 外那个 $b
unset($b); // 甚至可以直接 unset 函数内的 $b
var_dump(isset($b)); // 函数 scope 内的 $b 已经不存在了
}
test_global_ref();
var_dump($b);
$b = &$f;
var_dump($b);
// output:
// string(4) "2222"
// bool(false)
// string(3) "111"
// string(4) "????"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment