Reference-PHP Manual notes

Source: Internet
Author: User
Tags php class
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.


  
   

只有函数定义中有引用符号&,在函数调用时没有引用符号&

引用返回

要返回引用,使用下面的语法。

 value;}}$obj = new foo;$myValue = & $obj->getValue();$obj->value = 2;echo $myValue;

注意,引用返回和参数传递不同,函数定义和函数调用(指出返回的是一个引用,而不是通常的一个拷贝)这两个地方都用&符号。

取消引用

unset()一个引用时,只是断开了变量名和变量内容之间的绑定,变量内容不被销毁。

引用定位

许多PHP的语法结构是通过引用机制实现的,比如global引用和$this指针。

当用global $var声明一个变量时,实际上是建立了一个到全局变量的引用,和$var = & $GLOBALS['var']作用是相同的。在对象方法中,$this永远是调用它的对象的引用。

(全文完)

以上就介绍了引用 - PHP手册笔记,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

  • Related Article

    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.