In PHP5, the attention problem references
/ ***************************** / Author: old Youth E_MAIL: wenadmin@sina.com / ****** ********************** /
The author is more stupid, which is wrong, where is wrong, you will bear it! -_-!
In the PHP4, the reference is implemented through a simple "&" symbol. In PHP5 ($ OBJ1 = $ OBJ2;) The default is a reference method, no need to deliberately add a "&" symbol, but PHP4 is still retained in PHP5
This uses the reference method.
But what is the difference between these two methods? Let us take a look at it.
The old reference method is implemented as follows (one of the following sections can run on PHP4 and PHP5): Code 1:
Class test {function get () {return "aaaaa";}} $ T1 = new test (); $ t2 = & $ t1; echo $ t1-> get (). ""; echo $ t2-> get () ""; "This variable $ T1 and $ T2 point to the same object, and they share the same object in memory.
Code 2:
Class test {function get () {return "aaaaa";}} $ t1 = new test (); $ t2 = & $ t1; unset ($ T1); ECHO ($ T1 === NULL)? "T1 is NULL "" T1 is not null. "; Echo $ T2-> get ()." ";> T1 variable After the unset, T1 turns NULL, but T2 still points to the original object.
Code 3:
Class test {function get () {return "aaaaa";}} $ t1 = new test (); $ t2 = & $ t1; $ t1 = null; echo ($ t1 === null)? "T1 is NULL. "" T1 is not null. "; ECHO ($ T1 === NULL)?" T2 is NULL. ":" T2 is not null. ";> T1 = NULL, T1 becomes NULL, but T2 I have also become NULL.
God, how can the results of T1 and T2? Toss for a long time, I want to understand that through "&" reference, it should not be called "reference", it should be called "inventory", the concept of the address The C language is very clear, and the writing method of the code is very similar. Start, I thought this and Java's object references are the same, stupid .............. .......what!.
Let's take a look at the reference in PHP5. Code 4:
Class test1 {public function aa () {echo "aaaaaaaaaaa";} public function __destruct () {Echo "Game over!";}} $ t1 = new test1 (); $ t2 = $ t1; $ t1 = null; echo $ T2-> aa (); unset ($ T2);?>
Code 5:
Class test1 {public function aa () {echo "aaaaaaaaaaa";} public function __destruct () {echo "game over!";}} $ t1 = new test1 (); $ t2 = $ t1; unset ($ T1); Echo $ T2-> aa (); unset ($ T2);?> Found There is no, code 4 and code 5 run the result, this result is satisfactory, and the implementation of Java's object is finally consistent. .
-------------------------------------------------- Conclusion, the results prove that "&" in PHP4 is "to address", and PHP5's default approach is "reference" (). When references the object in PHP5, it is necessary to choose the appropriate reference according to the actual situation. The way. If the program is relatively large, reference the same object of the same object, and you want to handmade it after the object is used, and the individual is recommended to use the "&" mode, then clear it with $ var = null. Other time PHP5's default method. In addition, PHP5 is passed on the passage of large arrays, it is recommended to use "&" mode, after all, save memory spaces.