Reference-PHP manual notes

Source: Internet
Author: User
: 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$aAnd$bPoint 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,newThe operator automatically returns a reference.

Reference transfer

You can use the following code to transmit variables by referencing them,fooVariable in function$varPointing 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, suchglobalReference and$thisPointer.

When usingglobal $varWhen 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,$thisIt 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.

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.