Php variable reference and object reference

Source: Internet
Author: User
Tags what php
This article summarizes how to reference and reference variables in php? Next we will introduce the usage of php variable reference. Reference what PHP references allow

This article summarizes how to reference and reference variables in php? Next we will introduce the usage of php variable reference.

Reference

PHP references allow two variables to point to the same content, which means that the code is as follows:

, Which means that $ a and $ B point to the same variable.

$ A and $ B are exactly the same here. this is not because $ a points to $ B or vice versa, but $ a and $ B point to the same place. if a referenced array is copied, its value is not removed from the reference. this is also true for the array to be passed to the function. If an undefined variable is referenced, assigned, referenced, or referenced, the variable is automatically created.

Use reference for Undefined Variables. the code is as follows:

  1. Function foo (& $ var ){}
  2. Foo ($ a); // $ a is "created" and assigned to null
  3. $ B = array ();
  4. Foo ($ B ['B']);
  5. Var_dump (array_key_exists ('B', $ B); // bool (true)
  6. $ C = new StdClass;
  7. Foo ($ c-> d );
  8. Var_dump (property_exists ($ c, 'D'); // bool (true)
  9. ?>

The same syntax can be used in functions, which return references, and in the new operator (PHP 4.0.4 and later versions). The code is as follows:

  1. $ Bar = & new fooclass ();
  2. $ Foo = & find_var ($ bar );
  3. ?>

Since PHP 5, new automatically returns a reference, so the = & out-of-date message will be generated at the E_STRICT level, and a copy of the object will be generated without the & operator, if $ this is used in the class, it will act on the current instance of the class, and the instance (such as an object) will be copied without an & assignment, and $ this will act on the copy, this is not always the expected result. due to performance and memory consumption problems, I usually only want to work on an instance.

Although the @ operator can be used to suppress any error information in the constructor, such as @ new, it does not work when the & new statement is used, this is a limitation of the Zend Engine and may cause a parsing error.

PHP reference allows you to use two variables to point to the same content. the code is as follows:

  1. $ A = "ABC ";
  2. $ B = & $;
  3. Echo $ a; // output here: ABC
  4. Echo $ B; // output here: ABC
  5. $ B = "EFG ";
  6. Echo $ a; // Here, the value of $ a is changed to EFG, So EFG is output.
  7. Echo $ B; // output EFG here
  8. ?>

I will not talk much about the function's address transfer call. the following code is provided:

  1. Function test (& $)
  2. {
  3. $ A = $ a + 100;
  4. }
  5. $ B = 1;
  6. Echo $ B; // output 1
  7. Test ($ B); // here, $ B actually transmits the memory address of the variable content of $ B to the function, you can change the value of $ B by changing the value of $ a in the function.
  8. Echo"
    ";
  9. Echo $ B; // output 101

Note that test (1); will cause an error.

Function Reference return, first look at the code:

  1. Function & test ()
  2. {
  3. Static $ B = 0; // declare a static variable
  4. $ B = $ B + 1;
  5. Echo $ B;
  6. Return $ B;
  7. }
  8. $ A = test (); // This statement outputs the value of $ B as 1.
  9. $ A = 5;
  10. $ A = test (); // This statement outputs the value of $ B to 2.
  11. $ A = & test (); // This statement outputs the value of $ B to 3.
  12. $ A = 5;
  13. $ A = test (); // This statement outputs a value of 6 for $ B.

The following explains that $ a = test (); in this way does not actually get the function reference and return, which is no different from the normal function call. As for the reason, this is the PHP rule. PHP requires the $ a = & test (); method to obtain the function reference and return. what is the reference return? the PHP manual says: the return value of the reference is used when you want to use the function to find the variable on which the reference should be bound.

The example above is as follows:

$ A = test () is used to call a function. it only assigns the value of the function to $ a. any change made to $ a does not affect $ B in the function.

But how to call a function through $ a = & test, the function is to direct the memory address of the $ B variable in return $ B to the same place as the memory address of the $ a variable, that is, the equivalent effect ($ a = & B;) is generated, so the value of $ a is changed and the value of $ B is also changed, therefore, after $ a = & test (); $ a = 5; is executed, the value of $ B is changed to 5.

Static variables are used to help you understand the reference and return functions. In fact, function reference and return are mostly used in objects.

The code is as follows:

  1. Class {
  2. Var $ abc = "ABC ";
  3. }
  4. $ B = new;
  5. $ C = $ B;
  6. Echo $ B-> abc; // output ABC here
  7. Echo $ c-> abc; // output ABC here
  8. $ B-> abc = "DEF ";
  9. Echo $ c-> abc; // output DEF here
  10. ?>

The above code is the running effect in PHP5. in PHP5, object replication is implemented through reference. in the above column, $ B = new a; $ c = $ B; in fact, it is equivalent to $ B = new a; $ c = & $ B;

In PHP5, the object is called by reference by default, but sometimes you may want to create a copy of the object and expect that the change of the original object will not affect the copy. for this purpose, PHP defines a special method called _ clone.

Role of reference

If the program is large, there are many variables that reference the same object, and you want to manually clear the object after it is used up, I suggest using the & method, then clear it in the form of $ var = null. in other cases, use the default php5 method. in addition, we recommend that you use the "&" method for transferring large arrays in php5 to save memory space.

Cancel reference

When you unset a reference, you just disconnect the binding between the variable name and the variable content. This does not mean that the variable content has been destroyed, for example, the following code:

  1. $ A = 1;
  2. $ B = & $;
  3. Unset ($ );
  4. ?>

Not unset $ B, just $.

Global Reference

When a global $ var variable is declared, a reference to the global variable is actually created, that is, it is the same as doing so. The code is as follows:

This means that, for example, unset $ var does not unset global variables.

$ This

In the method of an object, $ this is always a reference to the object that calls it. The following is an episode.

In php, the address pointing (similar to pointer) function is not implemented by the user, but is implemented by the Zend core. in php, the reference uses the principle of "copy at Write, unless a write operation occurs, the variables or objects pointing to the same address will not be copied.

In layman's terms,

1: if the following code exists:

$ A = "ABC"; $ B = $ a; in fact, both $ a and $ B point to the same memory address, not $ a and $ B occupy different memory.

2: Add the following code on the basis of the above code:

$ A = "EFG"; because the memory data pointed to by $ a and $ B needs to be re-written, the Zend core automatically determines, a $ a data copy is automatically generated for $ B, and a new memory is applied for storage.

If a global variable is assigned to a reference within a function, the reference is only visible within the function. You can avoid this by using the $ GLOBALS array.

Reference Global variables in the function. the code is as follows:

  1. $ Var1 = "Example variable ";
  2. $ Var2 = "";
  3. Function global_references ($ use_globals)
  4. {
  5. Global $ var1, $ var2;
  6. If (! $ Use_globals ){
  7. $ Var2 = & $ var1; // visible only inside the function
  8. } Else {
  9. $ GLOBALS ["var2"] = & $ var1; // visible also in global context
  10. }
  11. }
  12. Global_references (false );
  13. Echo "var2 is set to '$ var2 'n'"; // var2 is set''
  14. Global_references (true );
  15. Echo "var2 is set to '$ var2 'n'"; // var2 is set to 'example variable'
  16. ?>

Use global $ var as the abbreviation of $ var = & $ GLOBALS ['var']; to assign other references to $ var, which only changes the reference of local variables.

The following code shows an interview question:

  1. $ A = 1;
  2. $ X = & $;
  3. $ B = $ a ++;
  4. ?>

Q: What are the values of $ B and $ x? The answer to the php interview question is as follows:

$ B = 1; $ x = 2;

You can see how much you don't know, and how much you know about interview references and object references.

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.