Are objects in php a reference type?

This seems to be an extremely simple question, do I need to write a blog post? Dear officials, I thought so at first, but it really blinded your small eyes. Why don't you look at what a quote is?
$a = 10;
$b = &$a;
$b = 20;
var_dump($a,$b);
In this extremely simple code, $b is a reference to $a, which is also an alias. Modifying $b will change $a, and vice versa. This is the so-called reference. Isn't that the same for objects?
class A
{
public $ num = 10;
}
$a = new A();
$a->num = 20;
function demo($b)
{
$b[0] = 5;
var_dump($b);
}
demo($a)
var_dump($a);
You will find that the output is the same twice. Does this mean that when the object is used as a function parameter, it is passed by reference? At this point, I have to say that Qianfeng’s students are really diligent and good at asking questions. At first I thought it was a quotation, but my classmate suggested that if you modify the demo to:
function demo($b)
{
$b = new A();
var_dump($b);
}
The two outputs are not the same! Why is this? From the perspective of C language, any variable in php, including objects, are pointers. Objects as parameters are equivalent to double pointers, so when $b points to a brand new object, it does not affect $obj, so the object is used as a function parameter. It's not a reference pass! Such a group of diligent and inquisitive students, the whole level is different.

Guess you like

Origin blog.csdn.net/chengshaolei2012/article/details/72631110