1.2.121. Catch Overwrite Variable¶
The try/catch structure uses some variables that are also in use in this scope. In case of a caught exception, the exception will be put in the catch variable, and overwrite the current value, loosing some data. It is recommended to use another name for these catch variables.
<?php
// variables and caught exceptions are distinct
$argument = 1;
try {
methodThatMayRaiseException($argument);
} (Exception $e) {
// here, $e has been changed to an exception.
}
// variables and caught exceptions are overlapping
$e = 1;
try {
methodThatMayRaiseException();
} (Exception $e) {
// here, $e has been changed to an exception.
}
?>
1.2.121.1. Connex PHP features¶
1.2.121.1.1. Suggestions¶
Use a standard : only use $e (or else) to catch exceptions. Avoid using them for anything else, parameter, property or local variable.
Change the variable, and keep the caught exception
1.2.121.1.2. Specs¶
Short name |
Structures/CatchShadowsVariable |
Rulesets |
|
Exakat since |
0.8.4 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
Very high |
ClearPHP |
|
Examples |
|
Available in |