0

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 undefined 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

brunoais
  • 6,258
  • 8
  • 39
  • 59
  • idk what you mean by "if's are slow" – Ilia Choly Sep 27 '11 at 17:08
  • If isset makes your application so slow it's unusable, I think you have other issues... – Jonnix Sep 27 '11 at 17:10
  • idk what you mean by "if's are slow" if then else for see, is slow by nature because it requires a jump. and using 200-500 ifs instead of 6 - 9 is really really noticeable in a php script @JonStirling it's not unusable but it's noticeably slower. And if it is used by 400-700 people at the same time it can be a huge problem. – brunoais Sep 27 '11 at 17:16
  • Yes, a custom error handler seems to be your best option then. There is no PHP builtin feature to only handle/log one kind of notices/warnings. (Regarding performance lots of logging won't be speedier than isset-chains. Probably more readable, yes.) – mario Sep 27 '11 at 17:29
  • `set_error_handler()` is the best option – tttony Sep 27 '11 at 17:40
  • @tttony is there a place where I can get a better explanation how can I solve this problem than the one in php.net? if there is, where? – brunoais Sep 27 '11 at 17:46

2 Answers2

1

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.

Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
  • No can do. Exceptions do not work with arrays in PHP. Already tried months ago in another code and it didn't work. – brunoais Sep 27 '11 at 17:08
  • 1
    Yes they do if you set error reporting for notices to be errors – Jonnix Sep 27 '11 at 17:13
  • if you don't want to do that, you can throw your own exceptions. This won't make your code faster, but it will at least be cleaner. – Ilia Choly Sep 27 '11 at 17:17
  • anyway, using try catch 200-700 times per script is also very slow and creates a lot of repetitive code – brunoais Sep 27 '11 at 17:19
  • that's a problem with your logic, not the language. Btw, 200-700 isn't that much. – Ilia Choly Sep 27 '11 at 17:20
  • if I make that I may increase the file size with 1MB more than it would if I got a simpler way with less repetitive code – brunoais Sep 27 '11 at 17:22
0

Consider putting your code into try-catch statements. Within the catch you can log the error.

http://php.net/manual/en/language.exceptions.php

evasilchenko
  • 1,862
  • 1
  • 13
  • 26
  • You're right. Did you take a look at converting the error to an exception which can be caught by the try-catch? http://snippets.dzone.com/posts/show/1617 – evasilchenko Sep 27 '11 at 17:21