note 113368 added to language.exceptions
| From: | cyrus+php at boadway dot ca | Date: | Wed, 02 Oct 2013 14:50:54 +0000 |
| Subject: | note 113368 added to language.exceptions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-196446@lists.php.net to get a copy of this message | ||
There's some inconsistent behaviour associated with PHP 5.5.3's finally and return
statements. If a method returns a variable in a try block (e.g. return $foo;), and finally modifies
that variable, the /modified/ value is returned. However, if the try block has a return that has to
be evaluated in-line (e.g. return $foo+0;), finally's changes to $foo will /not/ affect the
return value.
[code]
function returnVariable(){
$foo = 1;
try{
return $foo;
} finally {
$foo++;
}
}
function returnVariablePlusZero(){
$foo = 1;
try{
return $foo + 0;
} finally {
$foo++;
}
}
$test1 = returnVariable(); // returns 2, not the correct value of 1.
$test2 = returnVariablePlusZero(); // returns correct value of 1, but inconsistent with $test1.
[/code]
It looks like it's trying to be efficient by not allocating additional memory for the return
value when it thinks it doesn't have to, but the spec is that finally is run after try is
completed execution, and that includes the evaluation of the return expression.
One could argue (weakly) that the first method should be the correct result, but at least the two
methods should be consistent.
----
Server IP: 69.147.83.199
Probable Submitter: 98.14.204.53
----
Manual Page -- http://php.net/manual/en/language.exceptions.php
Edit -- https://master.php.net/note/edit/113368
Del: integrated -- https://master.php.net/note/delete/113368/integrated
Del: useless -- https://master.php.net/note/delete/113368/useless
Del: bad code -- https://master.php.net/note/delete/113368/bad+code
Del: spam -- https://master.php.net/note/delete/113368/spam
Del: non-english -- https://master.php.net/note/delete/113368/non-english
Del: in docs -- https://master.php.net/note/delete/113368/in+docs
Del: other reasons-- https://master.php.net/note/delete/113368
Reject -- https://master.php.net/note/reject/113368
Search -- https://master.php.net/manage/user-notes.php