: This article mainly introduces reference-PHP manual notes. if you are interested in the PHP Tutorial, refer to it. Reference
The reference in PHP means that different variable names are used to access the same variable content, similar to the Unix file name and file itself (the variable name is a directory entry, and the variable content is the file itself, access the same file using different directory entries). It can be seen as a hard link in a Unix file system.
There are two types of links in the file system: Hard Link and Symbolic Link ). By default, the ln command generates a hard link. Hard connection refers to the connection through the index node. In a Linux file system, a file stored in a disk partition is assigned with an index node number (InodeIndex) no matter what type it is ). In Linux, multiple file names direct to the same index node. Generally, this type of connection is hard connection. A hard connection allows a file to have multiple valid path names. in this way, you can create a hard connection to an important file to prevent "accidental deletion. The reason is as described above, because there is more than one connection to the index node of the Directory. Deleting only one connection does not affect the connection between the index node and other nodes. only when the last connection is deleted will the connection of the data block and Directory of the file be released. That is to say, the object will be deleted. In contrast to hard connection, there is another connection in the Lnux system, called Symbilc Link. Soft-link files are similar to Windows shortcuts. It is actually a type of special file. In a symbolic connection, a file is actually a text file containing the location information of another file.
References are mainly used to do three things:
- Point to the same content
- Pass variables with reference
- Reference return
Point to the same content
The reference allows two variables to point to the same content. for example$a = & $b
, Meaning$a
And$b
Point to the same variable.
If an undefined variable is referenced, assigned, referenced, or returned, the variable is automatically created. For example, the following program automatically creates a variable.$a
.
function foo(&$var) {}foo($a);
From PHP 5,new
The operator automatically returns a reference.
Reference transfer
You can use the following code to transmit variables by referencing them,foo
Variable in function$var
Pointing to and$a
..
Only the function definition has a reference symbol&
, There is no reference symbol in the function call.&
.
Reference return
To return a reference, use the following syntax.
value;}}$obj = new foo;$myValue = & $obj->getValue();$obj->value = 2;echo $myValue;
Note: different from parameter transfer, both function definition and function call (indicating that a reference is returned, rather than a normal copy) are used.&
Symbol.
Cancel reference
Whenunset()
During a reference, only the binding between the variable name and the variable content is broken, and the variable content is not destroyed.
Reference and positioning
Many PHP syntax structures are implemented through the reference mechanism, suchglobal
Reference and$this
Pointer.
When usingglobal $var
When declaring a variable, a reference to the global variable is actually created, and$var = & $GLOBALS['var']
The role is the same. In the object method,$this
It is always a reference to the object that calls it.
(Full text)
The above introduces reference-PHP manual notes, including the content, and hope to be helpful to friends who are interested in PHP tutorials.