Undefined Index - Design Error?
Posted: Sun Mar 06, 2005 5:00 pm
For any person who runs full error reporting, the warning produced by Undefined Variable and Undefined Index are some of the biggest plagues upon the web. As an novice programmer, I think that I've fallen into this trap: my scripts are inundated with a river of Warnings thrown by Undefined variables when I set Error Reporting on high.
However, it's not a simple case of forgetting to set $i = 0 when I start a simple loop. The way my program works, it loads a few, special of variables into the global namespace in names such as $sinf, $sc etc. These are arrays that contain more information. Then, I operate on the information, manipulating it for the main display. There's a lot of tricky stuff going on.
The problem is that I can't be exactly sure what values will be loaded and which will not. Currently, the program is written on the assumption that variables in an array that are undefined contain empty values. Some problems are that when I'm testing to see whether or not a particular variable is empty or not, when it's entered into a function that tests for definedness, they run into problems, because PHP throws an error if you even refer an undefined index to a function.
I'm starting to think that this is a fundamental design flaw, that I shouldn't have constructed the logic this way, but I don't want to rewrite my entire script. I've been thinking about a few solutions, such as:
However, it's not a simple case of forgetting to set $i = 0 when I start a simple loop. The way my program works, it loads a few, special of variables into the global namespace in names such as $sinf, $sc etc. These are arrays that contain more information. Then, I operate on the information, manipulating it for the main display. There's a lot of tricky stuff going on.
The problem is that I can't be exactly sure what values will be loaded and which will not. Currently, the program is written on the assumption that variables in an array that are undefined contain empty values. Some problems are that when I'm testing to see whether or not a particular variable is empty or not, when it's entered into a function that tests for definedness, they run into problems, because PHP throws an error if you even refer an undefined index to a function.
I'm starting to think that this is a fundamental design flaw, that I shouldn't have constructed the logic this way, but I don't want to rewrite my entire script. I've been thinking about a few solutions, such as:
- Taking all the functions that utilize the variable variables, test out the variable pool and change the arguments based on what's set and what's not. But I use a lot of functions, and this will cause a lot of code creep and extra stuff
Taking the script that creates the variables from the database and assigning blank values (but not undefined) to the variables that are most commonly assigned as arguments. This might not work if I use very specific variables for some functions (which means I may have to redefine the arguments so that they only take the general variables)