Home > Article > Backend Development > An explanation of PHP references
when used as follows: $ret = &bar(); That is, the ampersand must be added when defining and using it. 5, assignment and transfer of objects Note: There is a difference in the assignment and passing of object resources in PHP4 and PHP5. In PHP4:
PHP5:
Remarks: Found during testing: After $a = new Object(); $b = $a, if $a->attr = 12; $b->attr = 13, the final result is that both $a->attr and $b->attr are equal to 13, Rather than being independent as imagined. It may be that $a->attr and $b->attr are not deeply copied, but are still different mappings of the same memory. If $a = new Object(); $b = &$a;, there is no such doubt, because there is only one real object from beginning to end, and $a and $b are just mappings. 6, unset and =null The results of using unset($a) and $a=null are different. If this block of memory only has one mapping of $a, then unset($a) is equivalent to $a=null, and the reference count of the memory becomes 0 and is automatically recycled; If the block of memory has two mappings of $a and $b, then unset($a) will cause $a=null and $b remains unchanged, and $a=null will cause $a=$b=null. . Cause analysis: Assigning a variable to null will cause the reference count of the memory block corresponding to the variable to be directly set to 0 and automatically recycled. |