why is the code behaving this way WRT reference return?
This is my setting:
display_startup_errors = on
display_errors = On
error_reporting = E_ALL | E_STRICT
$b;
function func ($name) {
global $b;
$b = 10;
return $b;
}
$a =& func("myname");
++$a ;
echo '<br/>$a= '.$a.' $b= ' .$b."<br/>";
xdebug_debug_zval('a'); echo "<br/> ";
The above code output's the following notice:
Strict standards: Only variables should be assigned by reference in /path/to/file/file.php on line 'some line number'
$a= 11 $b= 10
a: (refcount=1, is_ref=0)=11
Why is the above code displaying the notice? And why is there a COW (copy on write) taking place?
$b;
function &func ($name) {//change here: to return a reference.
global $b;
$b = 10;
return $b;
}
$a =& func("myname");
++$a ;
echo '<br/>$a= '.$a.' $b= ' .$b."<br/>";
xdebug_debug_zval('a'); echo "<br/> ";
The above code will output:
$a= 11 $b= 11
a: (refcount=1, is_ref=1)=11
Why is no strict standards notice thrown here? And here the reference works.
$b;
function &func ($name) {
global $b;
$b = 10;
return $b;
}
$a = func("myname"); //change here: removed &
++$a ;
echo '<br/>$a= '.$a.' $b= ' .$b."<br/>";
xdebug_debug_zval('a'); echo "<br/> ";
The above code will output:
$a= 11 $b= 10
a: (refcount=1, is_ref=0)=11
Why does a COW take place here?
For info on xdebug_debug_zval visit here.
&
operator in PHP as reference is removed in the recent versions. Specially from PHP5. Now all reference type data (class etc) are automatically passed by reference.
Read Returning References and Passing by Reference
Your second snippet is working correctly.
In PHP if you want to return by reference, you have to write use &
operator in definition as well while assigning. see PHP reference Doc. Therefore, 2nd has correct syntax and it is working without giving any warning.
First snippet is giving warning as you haven't defined that this function will be returning reference and still you are assigning its return value by reference. So, it will not work. (see a
's value is incremented to 11 but b
is 10 only.)
In Third snippet you aren't assigning reference so it is not giving any warning. (But because of this reference will not work. (see difference in a
's and b
's value)
How the hell did I miss this: This completely mutes my question! from Returning References at PHP manual
Note: If you try to return a reference from a function with the syntax: return ($this->value); this will not work as you are attempting to return the result of an expression, and not a variable, by reference. You can only return variables by reference from a function - nothing else. Since PHP 4.4.0 in the PHP 4 branch, and PHP 5.1.0 in the PHP 5 branch, an E_NOTICE error is issued if the code tries to return a dynamic expression or a result of the new operator.
To use the returned reference, you must use reference assigment:
<?php
function &collector() {
static $collection = array();
return $collection;
}
$collection = &collector();
$collection[] = 'foo';
?>
链接地址: http://www.djcxy.com/p/58920.html
上一篇: MAMP php5严格标准
下一篇: 为什么代码以这种方式表现WRT引用返回?