Introduction to PHP variable references and object references _php tutorial

Source: Internet
Author: User
Tags what php
This article summarizes how to make variable references and variable references in PHP. How to do this, let us introduce the use of PHP variable reference.

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

The code is as follows Copy Code
$a =& $b;
?>


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

$a and $b are exactly the same here, not $a point 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 dereferenced. This is true for arrays that pass values to functions.

Note:

If a reference is assigned to an undefined variable, a reference parameter is passed, or a reference is returned, the variable is created automatically.

Example #1 use references to undefined variables

The code is as follows Copy Code

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, it returns references, and is used in the new operator (PHP 4.0.4 and later versions):

The code is as follows Copy Code
$bar =& new Fooclass ();
$foo =& Find_var ($bar);
?>

Since PHP 5, new automatically returns references, so using =& here is obsolete and generates E_STRICT-level messages.

Note:

Using the & operator causes the object to generate a copy. If $this in the class, it will be used for the current instance of the class. No assignment with & will copy this instance (such as an object) and $this will act on the copy, which is not always the desired result. Because of problems with performance and memory consumption, you usually just want to work on one instance.

Although you can use the @ operator to suppress any error information in the constructor, such as @new, this does not work with the &new statement. This is a limitation of the Zend engine and can result in a parsing error.


PHP references allow you to use two variables to point to the same content

The code is as follows Copy Code

$a = "ABC";
$b =& $a;
echo $a;//output here: ABC
echo $b;//output here: ABC
$b = "EFG";
echo $a;//The value of $ A here becomes EFG so the output EFG
echo $b;//Output EFG here
?>


Invocation of a function's address
Address call I'm not going to say much. Directly below the code

The code is as follows Copy Code

Function test (& $a)
{
$a = $a +100;
}
$b = 1;
echo $b;//Output 1
Test ($b); Here $b passed to the function is actually the memory address of the variable content of $b, by changing the value of $ A in the function can change the value of $b
echo "
";
echo $b;//Output 101

It is important to note that here Test (1), the words will be wrong, reason to think

A reference to the function returns
Look at the code first

The code is as follows Copy Code

function &test ()
{
Static $b =0;//declaration of a statically variable
$b = $b +1;
Echo $b;
return $b;
}

$a =test ();//This statement outputs a value of $b of 1
$a = 5;
$a =test ();//This statement outputs a value of $b of 2

$a =&test ();//This statement outputs a value of $b of 3
$a = 5;
$a =test ();//This statement outputs a value of $b of 6

The following explanation:
In this way $a=test (); The result is not a function reference return, which is not the same as a normal function call. The reason: It's php's rule.
PHP rules through $a=&test (); The way to get is the reference to the function is returned
As for what is a reference return (the PHP manual says that reference return is used when you want to use a function to find out which variable the reference should be bound to.) I haven't read this bullshit.

In the example above, the explanation is
$a =test () call the function, just assign the value of the function to $ A, and no change to $ A will affect the $b in the function.
by $a=&test (), the function is to call the memory address of the $b variable in the return $b to the same place as the memory address of the $ A variable.
That produces the equivalent effect ($a =&b;) So changing the value of $ A also changes the value of the $b so that it executes the
$a =&test ();
$a = 5;
Later, the value of the $b becomes 5

This is to let you understand that the function reference returns only use static variables, in fact, the function of the reference return is more used in the object

References to Objects

The code is as follows Copy Code

Class a{
var $abc = "abc";
}
$b =new A;
$c = $b;
echo $b->abc;//here output ABC
echo $c->abc;//here output ABC
$b->abc= "DEF";
echo $c->abc;//here output def
?>

The above code is the result of running in PHP5
The replication of objects in PHP5 is done by reference. The above $b=new A; $c = $b; is actually equivalent to $b=new A; $c =& $b;
The default in PHP5 is to invoke the object by reference, but sometimes you might want to make a copy of the object, and you want the original object to change without affecting the copy. For this purpose, PHP defines a special method called __clone.

The role of references
If the program is larger, referencing the same object is more variable, and you want to use the object after the manual removal of it, the personal suggestion "&" method, and then $var=null to clear. At other times, it's the default way to use PHP5. In addition, in the PHP5 for large arrays of delivery, it is recommended to use "&" mode, after all, saving memory space to use.


Dereference
When you unset a reference, you just break the binding between the variable name and the variable content. This does not mean that the contents of the variable are destroyed. For example:

The code is as follows Copy Code

$a = 1;
$b =& $a;
unset ($a);
?>

Not unset $b, just $a.


Global references
When you declare a variable with the global $var, you actually establish a reference to the global variable. In other words, it is the same as doing this:

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

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

$this
In the method of an object, $this is always a reference to the object that called it.


Here's a little episode.
PHP in the direction of the address (similar to the pointer) function is not implemented by the user itself, is implemented by the Zend Core, PHP refers to the use of "copy-on-write" principle, that is, unless a write operation, the same address to the variable or object is not copied.

The popular Speaking
1: If you have the following code

$a = "ABC";
$b = $a;

In fact, $a and $b both point to the same memory address and not $ A and $b occupy different memory

2: If you add the following code based on the above code

$a = "EFG";

Since $ A and $b point to the memory of the data to be re-written once, at this time Zend core will automatically determine to automatically generate a $ A copy of the data for $b, re-request a piece of memory for storage


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

Example #2 referencing global variables within a function

copy code

"!--? php
$var 1 = "Example variable"; br> $var 2 = "";

Function global_references ($use _globals)
{
Global $var 1, $var 2;
if (! $use _globals) {
$var 2 =& $ var1; Visible only inside the function
} else {
$GLOBALS ["var2"] =& $var 1;//visible also in global context
}
}

Global_references (false);
echo "VAR2 is set to ' $var 2 ' n"; VAR2 is set to '
Global_references (true);
echo "VAR2 is set to ' $var 2 ' n"; VAR2 is set to ' Example variable '
?

$var the global; As $var =& $GLOBALS [' var ']; The shorthand. Thus assigning other references to $var only changes the reference to the local variable.


I'm looking at an interview topic.


PHP face questions are as follows:

The code is as follows Copy Code
$a = 1;
$x =& $a;
$b = $a + +;
?>

Ask:
What are the values of $b and $x?

The answers to the PHP interview questions are as follows:
$b = 1;
$x = 2;
You see how much you figure out, see how much you know about the interview references and the object references.

http://www.bkjia.com/PHPjc/631279.html www.bkjia.com true http://www.bkjia.com/PHPjc/631279.html techarticle This article summarizes how to make variable references and variable references in PHP. How to do this, let us introduce the use of PHP variable reference. Reference to do what PHP cited ...

  • 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.