I'm making a program in a team of programmers and it has been decided that for a specific system we'll be using an array. In the majority of the uses, all indexes in the array will be properly set still, undefined indexes can happen. Using isset() or array_key_exists() will make the code too slow (because we will need lots of if's and and if is slow) and too "dirty" (too much code and repetitive code) so both are not an option. I already spotted the set_error_handler() function but I also don't know if it's the best option.
Primary objective: When that specific array causes a undefine开发者_JAVA百科d index it must be caught, solved (write situation to the logs) and the script must continue like nothing happened. What's the best way to do that?
NOTE: If any other error or warning happens I want PHP to treat it like it's used to, I only want this stuff made to that specific array with that name.
I hope I was clear enugh
you should use Exception
's
try{
if(!isset($values[23])) throw new Exception("index not defined");
// do dangerous stuff here
}catch(Exception $ex){
//handle error
}
however, the ideal solution would be to make sure this never happens in the first place.
Consider putting your code into try-catch statements. Within the catch you can log the error.
http://php.net/manual/en/language.exceptions.php
精彩评论