Php variable reference and object reference _ PHP Tutorial

Source: Internet
Author: User
Tags php introduction
Php variable reference and object reference. This article summarizes how to reference and reference variables in php? Next we will introduce the usage of php variable reference. The reference to PHP summarizes how to make a variable reference and what is a variable reference in php? Next we will introduce the usage of php variable reference.

Reference
PHP references allow two variables to point to the same content. This means that when you do this:

The code is as follows:
$ A = & $ B;
?>


This means that $ a and $ B point to the same variable.
Note:

$ A and $ B are exactly the same here. this doesn't mean $ a points to $ B or vice versa, but $ a and $ B point to the same place.

Note:

If an array with a reference is copied, its value is not removed from the reference. This is also true for passing values from arrays to functions.

Note:

If an undefined variable is referenced, assigned, referenced, or returned, the variable is automatically created.

Example #1 use reference for undefined variables

The code is as follows:

Function foo (& $ var ){}

Foo ($ a); // $ a is "created" and assigned to null

$ B = array ();
Foo ($ B ['B']);
Var_dump (array_key_exists ('B', $ B); // bool (true)

$ C = new StdClass;
Foo ($ c-> d );
Var_dump (property_exists ($ c, 'D'); // bool (true)
?>


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:
$ Bar = & new fooclass ();
$ Foo = & find_var ($ bar );
?>

Since PHP 5, new automatically returns a reference. Therefore, when = & is used here, messages at the E_STRICT level will be generated.

Note:

No & operator is used to generate a copy of the object. If $ this is used in the class, it will act on the current instance of the class. The instance (such as an object) will be copied without a value & and $ this will apply to this copy, which is not always the desired result. Due to performance and memory consumption problems, you usually only want to work on one 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:

$ A = "ABC ";
$ B = & $;
Echo $ a; // output here: ABC
Echo $ B; // output here: ABC
$ B = "EFG ";
Echo $ a; // Here, the value of $ a is changed to EFG, So EFG is output.
Echo $ B; // output EFG here
?>


Function address transfer call
I will not talk much about the address transfer call. the following code is provided directly.

The code is as follows:

Function test (& $)
{
$ A = $ a + 100;
}
$ B = 1;
Echo $ B; // output 1
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.
Echo"
";
Echo $ B; // output 101

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

Function Reference return
First look at the code

The code is as follows:

Function & test ()
{
Static $ B = 0; // declare a static variable
$ B = $ B + 1;
Echo $ B;
Return $ B;
}

$ A = test (); // This statement outputs the value of $ B as 1.
$ A = 5;
$ A = test (); // This statement outputs the value of $ B to 2.

$ A = & test (); // This statement outputs the value of $ B to 3.
$ A = 5;
$ A = test (); // This statement outputs a value of 6 for $ B.

The following explains:
In this way, $ a = test (); is not actually returned by the function reference, which is no different from the normal function call. The reason is: this is the PHP rule.
PHP requires that $ a = & test (); is used to obtain the function reference and return.
As for what is reference return (in the PHP Manual, reference return is used when you want to use a function to find the variable on which the reference should be bound .) I haven't understood this sentence for a long time.

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. Therefore, changing the value of $ a also changes the value of $ B.
$ A = & test ();
$ A = 5;
Later, 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.

Object reference

The code is as follows:

Class {
Var $ abc = "ABC ";
}
$ B = new;
$ C = $ B;
Echo $ B-> abc; // output ABC here
Echo $ c-> abc; // output ABC here
$ B-> abc = "DEF ";
Echo $ c-> abc; // output DEF here
?>

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; 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 is destroyed. For example:

The code is as follows:

$ A = 1;
$ B = & $;
Unset ($ );
?>

Not unset $ B, just $.


Global Reference
When a variable is declared with global $ var, a reference to the global variable is actually created. That is to say, it is the same as doing so:

The code is as follows:
$ Var = & $ GLOBALS ["var"];
?>

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.


// Next 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 = $;

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 that a $ a data copy is generated for $ B, apply for a new memory 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.

Example #2 reference global variables in the function

The code is as follows:

$ Var1 = "Example variable ";
$ Var2 = "";

Function global_references ($ use_globals)
{
Global $ var1, $ var2;
If (! $ Use_globals ){
$ Var2 = & $ var1; // visible only inside the function
} Else {
$ GLOBALS ["var2"] = & $ var1; // visible also in global context
}
}

Global_references (false );
Echo "var2 is set to '$ var2 'n'"; // var2 is set''
Global_references (true );
Echo "var2 is set to '$ var2 'n'"; // var2 is set to 'example variable'
?>

Use global $ var as the abbreviation of $ var = & $ GLOBALS ['var. Therefore, assigning other references to $ var only changes the reference of local variables.


See an interview question Next


Php interview questions are as follows:

The code is as follows:
$ A = 1;
$ X = & $;
$ B = $ a ++;
?>

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.

Why? Next we will introduce the usage of php variable reference. Reference PHP introduction...

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.