New to php, I have some questions about the instance code in the manual about the result of the last line. $ assigned = $ instance. Isn't this a copy, then the two objects should be two objects with the same content but different addresses. after copying them, they are assigned $ instance-> var = '$ assigned will have this value '; why does it affect $ assigned ??
Reply to discussion (solution)
The so-called value transfer means that only passing the object value to the target object is equivalent to copy; the system will re-open a completely identical memory space for the target object.
The so-called reference means passing the address of the object in the memory to the target object is equivalent to making the target object and the original object correspond to the same memory bucket. At this time, if you modify the target object, the data in the memory will also change.
Differences between value transfer and reference
The so-called value transfer means that only passing the object value to the target object is equivalent to copy; the system will re-open a completely identical memory space for the target object.
The so-called reference means passing the address of the object in the memory to the target object is equivalent to making the target object and the original object correspond to the same memory bucket. At this time, if you modify the target object, the data in the memory will also change.
Differences between value transfer and reference
I know the difference. I am surprised that $ assigned = $ instance is a value transfer. Why does one change and the other change.
My understanding:
Class SimpleClass {var $ test = 'test';} // The allocated memory address of the object instance is 1 $ instance = new SimpleClass (); var_dump ($ instance ); // Copy object instance value saved to memory address 2 saved to copy the same identifier returned by SimpleClass/$ instance = $ assigned = new returned id $ assigned = $ instance; var_dump ($ assigned); // pass the reference $ reference pointing to the memory address: 1 $ reference = & $ instance; var_dump ($ reference ); // changed the memory address 1 $ instance-> test = 'modify'; // because it is an address reference, $ reference is also null $ instance = null;
Manual description: in php5, an object variable no longer saves the value of the entire object. Only saves an identifier to access the real object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable is not referenced, but they all store copies of the same identifier, this identifier points to the real content of the same object.
My understanding:
Class SimpleClass {var $ test = 'test';} // The allocated memory address of the object instance is 1 $ instance = new SimpleClass (); var_dump ($ instance ); // Copy object instance value saved to memory address 2 saved to copy the same identifier returned by SimpleClass/$ instance = $ assigned = new returned id $ assigned = $ instance; var_dump ($ assigned); // pass the reference $ reference pointing to the memory address: 1 $ reference = & $ instance; var_dump ($ reference ); // changed the memory address 1 $ instance-> test = 'modify'; // because it is an address reference, $ reference is also null $ instance = null;
Manual description: in php5, an object variable no longer saves the value of the entire object. Only saves an identifier to access the real object content. When an object is passed as a parameter, returned as a result, or assigned to another variable, the other variable is not referenced, but they all store copies of the same identifier, this identifier points to the real content of the same object.
How does this identifier feel like the memory address function, so there is no difference between direct assignment and reference assignment?
Your code is too old, indicating that it is not in place
$ Instance is an object. From php5, all objects are transmitted as references.
So $ assigned = $ instance, after
$ Assigned and $ instance point to the same object. one attribute changes, and the other changes accordingly.
Your code is too old, indicating that it is not in place
$ Instance is an object. From php5, all objects are transmitted as references.
So $ assigned = $ instance, after
$ Assigned and $ instance point to the same object. one attribute changes, and the other changes accordingly.
Thank you for your explanation.