Currently I'm trying to clean up as much code as possible of the website of the company I work at, without like completely rewriting (stuff like removing dumb comments like // declare variables, clean variable naming, consistency etc.). The code is a huge mess and terrible, uses lots of files which gets included and variables get used between files which makes it really annoying.
The problem is, a simple thing as renaming a variable actually can break a lot. I currently have the following problem:
In file a I have a query, looking something like this:
$getProductSql = 'QUERY';
$getProduct = $db->prepare($getProductSql);
// some bind values etc.
$getProduct->execute();
$product = $getProduct->fetch(PDO::FETCH_ASSOC);
Now, after that file is included in the index, file b will get included which contains the following:
$getProductsSql = 'QUERY';
$getProducts = $db->prepare($getProductsSql);
// some bind values etc.
$getProducts->execute();
foreach ($getProducts->fetchAll(PDO::FETCH_ASSOC) as $product) {
    // some code
}
After that file is included, file c will be included which contains the following:
if ($product['COLUMN'] === '1')
In file c, the $product variable from file a should be used but due to how our structure is and file b getting included in between, $product from file a is replaced with the last value of $product from the loop in file b.
Is there any way to solve this without using 2 different variable names or moving code?
 
     
    