« 鹰的重生 | Main | 25 项男人都应该会的技能 »

引用传递

可以将一个变量通过引用传递给函数,这样该函数就可以修改其参数的值。语法如下:


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

$a=5;
foo($a);
// $a is 6 here
?>


注意在函数调用时没有引用符号――只有函数定义中有。光是函数定义就足够使参数通过引用来正确传递了。在最近版本的 PHP 中如果把 & 用在 foo(&$a); 中会得到一条警告说“Call-time pass-by-reference”已经过时了。

以下内容可以通过引用传递:


变量,例如 foo($a)

New 语句,例如 foo(new foobar())

从函数中返回的引用,例如:


function &bar()
{
$a = 5;
return $a;
}
foo(bar());
?>


详细解释见引用返回。


任何其它表达式都不能通过引用传递,结果未定义。例如下面引用传递的例子是无效的:


function bar() // Note the missing &
{
$a = 5;
return $a;
}
foo(bar()); // 自 PHP 5.1.0 起导致致命错误
foo($a = 5) // 表达式,不是变量
foo(5) // 导致致命错误
?>


这些条件是 PHP 4.0.4 以及以后版本有的。

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

About

This page contains a single entry from the blog posted on December 20, 2007 1:48 AM.

The previous post in this blog was 鹰的重生.

The next post in this blog is 25 项男人都应该会的技能.

Many more can be found on the main index page or by looking through the archives.

Creative Commons License
This weblog is licensed under a Creative Commons License.