[Php] references in PHP4

xiaoxiao2021-03-06  46

Quote is a very wonderful thing, there is a bit similar to the pointer in C, but it is different! In PHP, reference is just another name of the variable content. Below I have played a few more description (just php4 content, PHP5 has a big change in the use of classes and objects, and has not been studied):

1, $ a = & $ b; here created a reference $ A, he pointed to the variable content points to the variable of $ b, and Luo Wei is a little. A metaphor: Just have a cake than a drawer, B has a key, now B copies a key to A, A and B can open this drawer, eat this cake.

2, bind a return application to a variable: $ a = & new someclass (); $ a = & call_function (); new operator returns a copy of the instance of someClass, by reference operator, tied this copy Set to $ a, that is, $ a point to this instance. If you use assignment operattles =, then this instance will be copied, and $ a is to point to this new copy of this new replication.

3, the reference passes Function Somefunction (& $ a) {$ A = 10;} $ a = 10; SomeFuntion ($ a); // Now $ A 20 With this method, you can operate in the function range Variables. This is similar to the pointer in C. For example, I write a function that opens the file. Function OpenSomefile (& $ fd) {Global $ error; $ fd = @fopen ("somefile"); if ($ fd) {return true;} else {$ error = "can not open file";}} Then you can write a very knowledgeable program with C: // Initialize two variables $ fd = null; $ error = null; if (OpenSomeFile) {// do something} else {EXIT ($ Error); } Note that when you need to define a function, add the & symbol in front of the shape, and you will not need it in the place where you call this function. This is not the same as the reference below! 4. Reference Return Function & Foo () {// do something $ a = & new someclass (); returnid $ a;}

$ REFA = & foo (); here the function foo creates an object, $ A is the reference to this object, the function returns this reference. We want to use this object outside the function, you need to use this method, $ REFA = & foo (); this sentence binds the reference to the function foo to the variable $ refa, which is to say inside the REFA and FOO $ A points to the same content. Note that there must be & symbols here in the definition function and the call function. This approach is often used when using the Factory mode design program, such as PEAR :: DB library, etc. 5. When using unset ($ var) to destroy a reference, do not destroy $ VAR points to: $ a = 10; $ b = & $ A;

Echo $ a; echo "/ n"; echo $ b; echo "/ n"; unset ($ b); Echo $ A; UNSET ($ b), does not make $ A also change. Or drawers and keys, A and B have the key of the same drawer. B throws away the key, but A is still, or you can open the drawer. 6, I have just encountered a question: Function Foo (& $ a) {$ a = & new someclass ();} $ aaa = null; foo ($ AAA); this usage does not allow $ AAA to point to Someclass An example. why? Because when the foo is called, Guan $ A is a reference to $ AAA, = & $ AAA; then in the function, $ A is also re-pointing to another object. So, there is no operation of $ AAA. Just like B get the key to the drawer from A, but it is not enough, he will make this key to the drawer B, then he naturally can't open the drawer.

转载请注明原文地址:https://www.9cbs.com/read-89358.html

New Post(0)