search
Homephp教程php手册php的引用返回和取消引用

php的引用返回和取消引用

Jun 13, 2016 am 10:54 AM
phponefunctionCancelvariableandexistQuoteturn upofbindingreturn

一、引用返回

引用返回用在当想用函数找到引用应该被绑定在哪一个变量上面时。不要用返回引用来增加性能,引擎足够聪明来自己进行优化。仅在有合理的技术原因时才返回引用!要返回引用,使用此语法:

[php]  

class foo {  

    public $value = 42;  

  

    public function &getValue() {  

        return $this->value;  

    }  

}  

  

$obj = new foo;  

$myValue = &$obj->getValue(); // $myValue is a reference to $obj->value, which is 42.  

$obj->value = 2;  

echo $myValue;                // prints the new value of $obj->value, i.e. 2.  

?>  

 

以上是PHP Manual给出的解释并表示太好理解

[php]  

    function &test(){  

        static $b = 0;  //声明一个静态变量  

        $b = $b+1;  

        echo $b."
";  

        return $b;  

    }  

   $a = test();  //输出 $b 的值为:1  

   $a = 5;  

   $a = test();  //输出 $b 的值为:2  

    

   $a = &test();  //输出 $b 的值为:3  **注意**  

   $a = 5;    //$b的值变为了5  

   $a = test();  //输出 $b 的值为:6  **注意**  

   ?>  

 

 

$a = test() 虽然说函数定义的时候,是引用返回方式,但是如果采用这种普通形势调用函数,那它的作用也就和普通的函数一样,所以看结果就是1、2

 

$a = &test() 这种调用方式就是引用返回,就类似于 $a = &$b ,然后第二句又把$a = 5,那就是等于将变量$b = 5,最后一句得到的6也就很容易理解了!

 

和参数传递不同,这里必须在两个地方都用 & 符号——指出返回的是一个引用,而不是通常的一个拷贝,同样也指出 $a 是作为引用的绑定,而不是通常的赋值。www.2cto.com

 

Note: 如果试图这样从函数返回引用:return ($this->value);,这将不会起作用,因为在试图返回一个表达式的结果而不是一个引用的变量。只能从函数返回引用变量——没别的方法。如果代码试图返回一个动态表达式或 new 运算符的结果,自 PHP 4.4.0 和 PHP 5.1.0 起会发出一条 E_NOTICE 错误。

 

二、取消引用

 

 

当 unset 一个引用,只是断开了变量名和变量内容之间的绑定。这并不意味着变量内容被销毁了。例如:

[php]  

$a = 1;  

$b =& $a;  

unset($a);  

?>  

不会 unset $b,只是 $a。

再拿这个和 Unix 的 unlink 调用来类比一下可能有助于理解。

 

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version