Reference-PHP Manual notes

Source: Internet
Author: User
Tags php class

Original: Reference-PHP Manual notes

What is a reference?

The reference in PHP means that the contents of the same variable are accessed with different variable names, similar to the UNIX file name and the file itself (the variable name is the directory entry, the variable content is the file itself, that is, accessing the same file with different directory entries), which can be seen as a hard link in the UNIX filesystem.

There are two types of links in the file system, one called hard link, and the other is called Symbolic link (symbolic). By default, the LN command produces a hard link. A hard join refers to a connection made through an index node. In a Linux file system, files saved in a disk partition are assigned a number, called the index node number (INODEINDEX), regardless of the type. In Linux, multiple file names point to the same index node that exists. In general, this connection is a hard connection. The purpose of a hard connection is to allow a file to have multiple valid pathname, so that users can establish a hard connection to important files to prevent "accidental deletion" of the function. The reason for this is as above, because there is more than one connection to the index node that should be the directory. Deleting only one connection does not affect the index node itself and other connections, and the connection to the file's data block and directory will be released only if the last connection is deleted. In other words, the file is actually deleted. In contrast to a hard connection, there is another connection in the Lnux system, called a symbolic connection (SYMBILC link), also known as a soft connection. A soft link file is a bit like a shortcut to Windows. It is actually a kind of special file. In a symbolic connection, a file is actually a text file that contains location information for another file.

References are mainly used to do three things:

    • Point to the same content
    • Passing variables by reference
    • Reference returns
Point to the same content

A reference allows two variables to be allowed to point to the same content, such as $a = & $b , meaning $a and pointing to the $b same variable.

If a reference is assigned to an undefined variable, a reference parameter is passed, or a reference is returned, the variable is created automatically. For example, the following program will automatically create variables $a .

function foo(&$var) {}foo($a);

Since PHP 5, the new operator automatically returns a reference.

Reference delivery

Passing variables by reference can be implemented in the following code, where the foo variables $var point to $a the same content as they point to.

<?php function foo(&$var) {$var++;}$a = 1;foo($a);echo $a;

Only a reference symbol is in the function definition & , and there is no reference symbol when the function is called & .

Reference returns

To return a reference, use the following syntax.

<?php class foo {public $value = 42;public function & getValue() {return $this->value;}}$obj = new foo;$myValue = & $obj->getValue();$obj->value = 2;echo $myValue;

Note that reference returns are different from parameter passing, function definitions and function calls (which indicate that a reference is returned instead of a typical copy) and that all two of these places are & signed .

Dereference

When unset() a reference is made, only the binding between the variable name and the variable content is broken, and the contents of the variable are not destroyed.

Reference positioning

Many of the syntax constructs of PHP are implemented by reference mechanisms, such as global references and $this pointers.

When global $var declaring a variable, you actually create a reference to the global variable, and the $var = & $GLOBALS[‘var‘] function is the same. In an object method, $this it is always a reference to the object that called it.

(End of full text)

Reference-PHP Manual notes

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.