时间:2021-07-01 10:21:17 帮助过:5人阅读
$a = 100;
$b = $a;
unset($a);
echo $b."\n";
有一些区别,在你的例子中,如果你给b赋值一个新的值,lh的指向也会跟着改变,但是a不会有这个问题。Note:
$a and $b are completely equal here. $a is not pointing to $b or vice versa. $a and $b are pointing to the same place.
对PHP5中对象和引用的说法是:
A PHP reference is an alias, which allows two different variables to write to the same value. As of PHP 5, an object variable doesn't contain the object itself as value anymore. It only contains an object identifier which allows object accessors to find the actual object. When an object is sent by argument, returned or assigned to another variable, the different variables are not aliases: they hold a copy of the identifier, which points to the same object.
其实对对象变量取&更像是一个指向“指针”的指针。
@李怀志 我也记得PHP里不需要加&引用符号,都是引用传递。但是经过我的测试(PHP5.6.7),我对以下内容存在疑惑:class Person {
public $name = 'myName';
}
$obj = new Person;
$assign = $obj;
$reference = & $obj;
$obj = null;//此操作很重要
var_dump($obj);//得到null
var_dump($assign);//得到object(Person)#1 (1) {["name"]=>string(6) "myName"}
var_dump($reference);//得到null
百度一下
完全一样