Passing/Returning 引用对象 + 更改对象无效
Passing/Returning references to object + changing object is not working
我正在使用 an answer from How to get random value out of an array to write a function that returns a random item from the array. I modified it to pass by reference and return a reference。
很遗憾,它似乎不起作用。对返回对象的任何修改都不会保留。我做错了什么?
我正在使用 PHP 5.4,如果有影响的话(不要问)。
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
$return = isset($array[$k])? $array[$k]: $default;
return $return;
}
用法...
$companies = array();
$companies[] = array("name" => "Acme Co", "employees"=> array( "John", "Jane" ));
$companies[] = array("name" => "Inotech", "employees"=> array( "Bill", "Michael" ));
$x = &random_value($companies);
$x["employees"][] = "Donald";
var_dump($companies);
输出...
array(2) {
[0] =>
array(2) {
'name' =>
string(7) "Acme Co"
'employees' =>
array(2) {
[0] =>
string(4) "John"
[1] =>
string(4) "Jane"
}
}
[1] =>
array(2) {
'name' =>
string(7) "Inotech"
'employees' =>
array(2) {
[0] =>
string(4) "Bill"
[1] =>
string(7) "Michael"
}
}
}
我什至复制并粘贴了文档中的示例,并且 none 中的示例都有效。他们都输出null
.
三元运算符创建一个隐式副本,这会破坏引用链。使用明确的 if... else
:
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
if (isset($array[$k])) {
return $array[$k];
} else {
return $default;
}
}
至于为什么,docs 现在状态:
Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
另见 this bug 三元运算符实际上 returns 在 foreach
上下文中引用,而实际上不应该这样做。
由于我刚刚吃了一顿丰盛的午餐,所以我无法思考比@bishop 更好的功能方面,但是这有效:
$x =& $companies[array_rand($companies)];
$x["employees"][] = "Donald";
var_dump($companies);
我正在使用 an answer from How to get random value out of an array to write a function that returns a random item from the array. I modified it to pass by reference and return a reference。
很遗憾,它似乎不起作用。对返回对象的任何修改都不会保留。我做错了什么?
我正在使用 PHP 5.4,如果有影响的话(不要问)。
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
$return = isset($array[$k])? $array[$k]: $default;
return $return;
}
用法...
$companies = array();
$companies[] = array("name" => "Acme Co", "employees"=> array( "John", "Jane" ));
$companies[] = array("name" => "Inotech", "employees"=> array( "Bill", "Michael" ));
$x = &random_value($companies);
$x["employees"][] = "Donald";
var_dump($companies);
输出...
array(2) {
[0] =>
array(2) {
'name' =>
string(7) "Acme Co"
'employees' =>
array(2) {
[0] =>
string(4) "John"
[1] =>
string(4) "Jane"
}
}
[1] =>
array(2) {
'name' =>
string(7) "Inotech"
'employees' =>
array(2) {
[0] =>
string(4) "Bill"
[1] =>
string(7) "Michael"
}
}
}
我什至复制并粘贴了文档中的示例,并且 none 中的示例都有效。他们都输出null
.
三元运算符创建一个隐式副本,这会破坏引用链。使用明确的 if... else
:
function &random_value(&$array, $default=null)
{
$k = mt_rand(0, count($array) - 1);
if (isset($array[$k])) {
return $array[$k];
} else {
return $default;
}
}
至于为什么,docs 现在状态:
Note: Please note that the ternary operator is an expression, and that it doesn't evaluate to a variable, but to the result of an expression. This is important to know if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a return-by-reference function will therefore not work and a warning is issued in later PHP versions.
另见 this bug 三元运算符实际上 returns 在 foreach
上下文中引用,而实际上不应该这样做。
由于我刚刚吃了一顿丰盛的午餐,所以我无法思考比@bishop 更好的功能方面,但是这有效:
$x =& $companies[array_rand($companies)];
$x["employees"][] = "Donald";
var_dump($companies);