php的变量引用详解
1 <?php 2 class SimpleClass 3 { 4 // property declaration 5 public $var = 'a default value'; 6 7 // method declaration 8 public function displayVar() { 9 echo $this->var; 10 } 11 } 12 13 14 15 $instance = new SimpleClass(); 16 17 $assigned = $instance; 18 $reference =& $instance; 19 20 $assigned->var = "aaaaaaa"; 21 22 $assigned = null; 23 24 var_dump($instance); 25 var_dump($reference); 26 var_dump($assigned); 27 28 29 /* 30 输出内容: 31 E:\php>php TestClass.php 32 class SimpleClass#1 (1) { 33 public $var => 34 string(7) "aaaaaaa" 35 } 36 class SimpleClass#1 (1) { 37 public $var => 38 string(7) "aaaaaaa" 39 } 40 NULL 41 42 总结: 43 $assigned, $reference, $assigned三个变量引用的都是同一块内存地址, 44 $assigned = null; 之后,断开了指向$assigned的指针,所以$assigned为空,其他的变量不变 45 /