A beginner in php, ask a question about the instance code in the manual-php Tutorial

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.