I have a script php code
<?php
class A
{
public $x;
}
$a = new A();
$a->x = 10;
$b = &$a;
$b = new A();
$b->x = 20;
echo $a->x;
When try to understand code I think the result is
10
But the result is: 20
I don't undersand what's going on!,
I also try with bing:
Hope someone can help me explain the code. Thank you :()
$b->x = 20;and as$bis a reference to$abecause you used&in$b = &$a;you are setting$a's value because they are now both the same object$bto a new instance, it didn't create a separate object but instead changed the reference of$bto the new instance. Since both$aand$bwere pointing to the same object, modifying the object through$bwill also affect$a.$bfor new reference$b = new A();at this code maybe$bhave not any ref to$abut I don't know why I change$b->xit also change ```$a````$b = &$a, that means both$aand$breference the same object in memory, so when you create a new instance using$b = new A(), you are replacing the original instance of objectA, because ANY change to$bwill also be made to$a, and vice versa, because they are referencing the same object in memory