Home > Backend Development > PHP Tutorial > 对象引用问题

对象引用问题

WBOY
Release: 2016-06-23 13:50:06
Original
921 people have browsed it

class A{	public $foo = 1;}$a = new A();$b = $a;$b->foo = 2;echo $a->foo . "<br />";$c = new A();$d = & $c;$d->foo = 3;echo $c->foo;
Copy after login

$b=$a 和 $d=&$c 有什么区别啊,这里用不用 & 都没什么不同


回复讨论(解决方案)

= 传值
=& 传引用
对象总是以 引用 传递的
所以对于对象 = 和 =& 没有区别

= 传值
=& 传引用
对象总是以 引用 传递的
所以对于对象 = 和 =& 没有区别


今天再看发现不对劲,但应该是有区别的吧?
<?phpclass A{}$a = new A();$b = &$a;$c = $a;$a = null;var_dump($a);var_dump($b);var_dump($c);
Copy after login

输出:NULL NULL object(A)#1 (0) { }
$c也应该为null才对啊??

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template