I just spent hours trying to debug an out of memory error caused by the following code:
for ($i = 1; i <= 4; $i++) {
$allowed[] = $type.'_'.$i;
}
Which PHP kindly mangles into:
for ($i = 1; 'i' <= 4; $i++) {
$allowed[] = $type.'_'.$i;
}
This causes an endless loop, which eventually leads to an out of memory error due to appending to the array. PHP will generate a notice level error, and I could change my error reporting level to show these but I am working on a third party application which has a tendency to generate enough of these that this isn’t really a viable solution.
Is there any way to trap these really simple bugs? Ironically, if you do something like constant('i') and explicitly ask for the value it will generate a warning rather than a notice, and this behaviour would be ideal.
You could create a custom error function then filter out the most common errors and only report the less common ones. Then up the error reporting level in PHP. E.g.
You could also filter errors like so:
Filter out all errors unless they are
in a file of interest by testing
$error_fileagainst an array offiles you maintain
Even better (not on a production
server) fetch the last_modified
date/time of
$error_filewithfilemtime()and report the error ifit was changed within the last 10
minutes. This helps you debug code as
you write it
Or if it’s within a framework which
yours sounds like it is, break apart
the path of
$error_fileand test ifit’s in your module/view/controller
files, as opposed to core framework
files