PHP: 为什么只能通过引用传递变量?
PHP: Why should only variables be passed by reference?
如果您是 PHP 开发人员,您很可能已经看到以下通知:
Notice: Only variables should be passed by reference in /somefile.php
on line xxx
(问题扩展在 Only variables should be passed by reference 中处理)
抛出通知示例:
$string = "hi-dude";
echo end(explode('-', $string));
工作示例:
$string = "hi-dude";
$strings = explode('-', $string);
echo end($strings);
解释:
Only real variables may be passed by reference, not functions which are returning the correct variable.
但是我想不出出现此通知的充分理由。感觉没有必要,有时需要我写很多额外的代码行。 PHP 有这个奇怪限制的原因是什么? 为什么会出现这个问题?
end()
或 array_pop()
将 return E_NOTICE
和消息
Only variables should be passed by reference
原因是end()
需要引用,因为它使得当前元素指针指向最后一个元素
一条线就可以搞定,
$string = "this-is-a-sample-text";
echo substr(strrchr($string, '-'), 1);
最后我找到了一个很好的解释,帮助我理解了这一点:What's the difference between passing by reference vs. passing by value?
正如 Daniel Pryden 所说:
In simplest terms:
- call by value means that you pass values as function arguments
- call by reference means that you pass variables as function arguments
In metaphoric terms:
- Call by value is where I write down something on a piece of paper and hand it to you. Maybe it's a URL, maybe it's a complete copy of
War and Peace. No matter what it is, it's on a piece of paper which
I've given to you, and so now it is effectively your piece of paper.
You are now free to scribble on that piece of paper, or use that piece
of paper to find something somewhere else and fiddle with it,
whatever.
- Call by reference is when I give you my notebook which has something written down in it. You may scribble in my notebook (maybe I
want you to, maybe I don't), and afterwards I keep my notebook, with
whatever scribbles you've put there. Also, if what either you or I
wrote there is information about how to find something somewhere else,
either you or I can go there and fiddle with that information.
在这种情况下,通知 "Only variables should be passed by reference" 仍然不合理 因为我们只对检索的最后一个值感兴趣大批。但是函数 end()
定义为
mixed end ( array &$array )
表示按引用传递的 & 符号存在是有原因的:end()
不仅仅是 return 数组的最后一个元素,它还将其内部指针更改为末尾.因此修改数组。
如果我们只 return 数组的最后一个元素而不触及数组,则不需要通过引用传递数组,我们也不会得到这个通知。但是 end()
在某种程度上是错误的函数。
如果我没有理由收到此通知怎么办?
请注意,要调用的函数也可能定义错误。在我的例子中,我有一个这样定义的函数:
/**
* Flatten an array by one level if only needing a certain key value from a sub array.
*
* Example: [["foo"=>"bar","foo"=>"cheese"]]
* Result: ["bar","cheese"]
*
* @param $array: The input array.
* @param $key: The key to flatupshift. Default is 0.
* @return $array: The result
*/
private function array_flatupshift(&$array, $key = 0) {
$a = [];
foreach ($array as $item) {
if (is_object($item)) {
array_push($a, $item->$key);
} else if (is_array($item)) {
array_push($a, $item[$key]);
}
}
return $a;
}
这只是一个错误的函数定义。 所以如果你也收到这样的通知:检查你调用的函数是否定义正确。按引用传递在这里没有意义,因为数组被通过不会受到任何影响。因此函数定义应该没有 "reference &/":
private function array_flatupshift($array, $key = 0) {
在某些情况下,如果您知道自己在做什么,则可以使用错误控制运算符。因此:
$string = "hi-dude";
echo @end(explode('-', $string));
... 将是 o.k。我想是不再需要爆炸的结果。但是请注意抑制所有可能错误的缺点。如果我这里说错了请指正。
如果您是 PHP 开发人员,您很可能已经看到以下通知:
Notice: Only variables should be passed by reference in /somefile.php on line xxx
(问题扩展在 Only variables should be passed by reference 中处理)
抛出通知示例:
$string = "hi-dude";
echo end(explode('-', $string));
工作示例:
$string = "hi-dude";
$strings = explode('-', $string);
echo end($strings);
解释:
Only real variables may be passed by reference, not functions which are returning the correct variable.
但是我想不出出现此通知的充分理由。感觉没有必要,有时需要我写很多额外的代码行。 PHP 有这个奇怪限制的原因是什么? 为什么会出现这个问题?
end()
或 array_pop()
将 return E_NOTICE
和消息
Only variables should be passed by reference
原因是end()
需要引用,因为它使得当前元素指针指向最后一个元素
一条线就可以搞定,
$string = "this-is-a-sample-text";
echo substr(strrchr($string, '-'), 1);
最后我找到了一个很好的解释,帮助我理解了这一点:What's the difference between passing by reference vs. passing by value?
正如 Daniel Pryden 所说:
In simplest terms:
- call by value means that you pass values as function arguments
- call by reference means that you pass variables as function arguments
In metaphoric terms:
- Call by value is where I write down something on a piece of paper and hand it to you. Maybe it's a URL, maybe it's a complete copy of War and Peace. No matter what it is, it's on a piece of paper which I've given to you, and so now it is effectively your piece of paper. You are now free to scribble on that piece of paper, or use that piece of paper to find something somewhere else and fiddle with it, whatever.
- Call by reference is when I give you my notebook which has something written down in it. You may scribble in my notebook (maybe I want you to, maybe I don't), and afterwards I keep my notebook, with whatever scribbles you've put there. Also, if what either you or I wrote there is information about how to find something somewhere else, either you or I can go there and fiddle with that information.
在这种情况下,通知 "Only variables should be passed by reference" 仍然不合理 因为我们只对检索的最后一个值感兴趣大批。但是函数 end()
定义为
mixed end ( array &$array )
表示按引用传递的 & 符号存在是有原因的:end()
不仅仅是 return 数组的最后一个元素,它还将其内部指针更改为末尾.因此修改数组。
如果我们只 return 数组的最后一个元素而不触及数组,则不需要通过引用传递数组,我们也不会得到这个通知。但是 end()
在某种程度上是错误的函数。
如果我没有理由收到此通知怎么办? 请注意,要调用的函数也可能定义错误。在我的例子中,我有一个这样定义的函数:
/**
* Flatten an array by one level if only needing a certain key value from a sub array.
*
* Example: [["foo"=>"bar","foo"=>"cheese"]]
* Result: ["bar","cheese"]
*
* @param $array: The input array.
* @param $key: The key to flatupshift. Default is 0.
* @return $array: The result
*/
private function array_flatupshift(&$array, $key = 0) {
$a = [];
foreach ($array as $item) {
if (is_object($item)) {
array_push($a, $item->$key);
} else if (is_array($item)) {
array_push($a, $item[$key]);
}
}
return $a;
}
这只是一个错误的函数定义。 所以如果你也收到这样的通知:检查你调用的函数是否定义正确。按引用传递在这里没有意义,因为数组被通过不会受到任何影响。因此函数定义应该没有 "reference &/":
private function array_flatupshift($array, $key = 0) {
在某些情况下,如果您知道自己在做什么,则可以使用错误控制运算符。因此:
$string = "hi-dude";
echo @end(explode('-', $string));
... 将是 o.k。我想是不再需要爆炸的结果。但是请注意抑制所有可能错误的缺点。如果我这里说错了请指正。