Hi,
I stumbled upon this code while working with a variable passed by
reference, I was wondering if someone could provide some feedback before
raising a ticket, just in case PHP (5.3.6) is doing what it's supposed to
do.
In the code below I would expect "catch" to change the variable as
an assignment would do, but that doesn't happen.
function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $exception) {
var_dump(gettype($exception));
echo $exception->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));
Expected:
string(6) "object"
foo error
string(6) "object"
Current result:
string(6) "object"
foo error
string(4) "NULL"
It looks like "catch" is creating a completely new variable in the scope,
also removing the existing one from it.
I appreciate this is an edge case, if not a bug is it worth adding it to
http://php.net/manual/en/language.exceptions.php or somewhere under
http://www.php.net/manual/en/language.references.php ?
Thank you!
Hi,
I stumbled upon this code while working with a variable passed by
reference, I was wondering if someone could provide some feedback before
raising a ticket, just in case PHP (5.3.6) is doing what it's supposed to
do.
In the code below I would expect "catch" to change the variable as
an assignment would do, but that doesn't happen.function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $exception) {
var_dump(gettype($exception));
echo $exception->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));Expected:
string(6) "object"
foo error
string(6) "object"Current result:
string(6) "object"
foo error
string(4) "NULL"It looks like "catch" is creating a completely new variable in the scope,
also removing the existing one from it.
Hi,
could you file a bug at bugs.php.net? :)
thanks
I appreciate this is an edge case, if not a bug is it worth adding it to
http://php.net/manual/en/language.exceptions.php or somewhere under
http://www.php.net/manual/en/language.references.php ?Thank you!
--
Laruence Xinchen Hui
http://www.laruence.com/
Hi,
I stumbled upon this code while working with a variable passed by
reference, I was wondering if someone could provide some feedback before
raising a ticket, just in case PHP (5.3.6) is doing what it's supposed to
do.
In the code below I would expect "catch" to change the variable as
an assignment would do, but that doesn't happen.function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $exception) {
var_dump(gettype($exception));
echo $exception->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));Expected:
string(6) "object"
foo error
string(6) "object"Current result:
string(6) "object"
foo error
string(4) "NULL"It looks like "catch" is creating a completely new variable in the scope,
also removing the existing one from it.
Hi:
after a deep look, I think it's not a bug.
the exception in the catch block is only a local var of that block.
I think you should change you codes like following to accomplish
your requirement:
function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $e) {
var_dump(gettype($e));
$exception = $e;
echo $e->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));
thanks
Hi,
could you file a bug at bugs.php.net? :)
thanks
I appreciate this is an edge case, if not a bug is it worth adding it to
http://php.net/manual/en/language.exceptions.php or somewhere under
http://www.php.net/manual/en/language.references.php ?Thank you!
--
Laruence Xinchen Hui
http://www.laruence.com/
--
Laruence Xinchen Hui
http://www.laruence.com/
Hi:
after a deep look, I think it's not a bug.the exception in the catch block is only a local var of that block.
Not sure this makes sense to me. PHP does not have block scopes; all
variables in a function are in the same scope. For example the
$exception variable will also be available after the catch block. So
to me it would make sense if the outer variable would be changed :)
Nikita
Hi Laruence,
the solution you suggest would work though it makes it all a bit more
verbose...
I noticed also that the original variable is completely removed from the
function scope, which is probably a bug.
I'll raise a ticket, thank you ;-)
Devis
Hi,
I stumbled upon this code while working with a variable passed by
reference, I was wondering if someone could provide some feedback before
raising a ticket, just in case PHP (5.3.6) is doing what it's supposed
to
do.
In the code below I would expect "catch" to change the variable as
an assignment would do, but that doesn't happen.function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $exception) {
var_dump(gettype($exception));
echo $exception->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));Expected:
string(6) "object"
foo error
string(6) "object"Current result:
string(6) "object"
foo error
string(4) "NULL"It looks like "catch" is creating a completely new variable in the
scope,
also removing the existing one from it.
Hi:
after a deep look, I think it's not a bug.the exception in the catch block is only a local var of that block.
I think you should change you codes like following to accomplish
your requirement:function foo(&$exception)
{
try {
throw new Exception('foo error');
} catch (Exception $e) {
var_dump(gettype($e));
$exception = $e;
echo $e->getMessage() . "\n";
}
}
foo($error);
var_dump(gettype($error));thanks
Hi,
could you file a bug at bugs.php.net? :)
thanks
I appreciate this is an edge case, if not a bug is it worth adding it to
http://php.net/manual/en/language.exceptions.php or somewhere under
http://www.php.net/manual/en/language.references.php ?Thank you!
--
Laruence Xinchen Hui
http://www.laruence.com/--
Laruence Xinchen Hui
http://www.laruence.com/