2

In my PHP layer I'm receiving error codes as strings ("NOT_FOUND", "EXPIRED", etc). It's a small list of possible strings, perhaps a dozen.

What's the most efficient way of dealing with these? Using switch-case against string constants stored in a class, or should I parse them to numbers (or something) first? Or is PHP smart enough so it doesn't really matter?

3
  • 4
    Don't worry about it! You're talking about differences of a handful of processor cycles at best, nothing you will ever notice. Write it the way that's most readable and maintainable. Commented Apr 12, 2011 at 8:40
  • @deceze Yeah you are probably right but using plain strings is a bad way to go if you ever have to change a value... using the static class constants a simple refractor should do it (hopefully ;) Commented Apr 12, 2011 at 8:43
  • 1
    @Michael That goes with what I wrote about maintainability. Performance shouldn't be the deciding factor. Commented Apr 12, 2011 at 8:45

3 Answers 3

3

You might want to consider using constants? Let's say you have a class Error and define all error codes there like this:

class Error {
   const NOT_FOUND = 0;
   const EXPIRED = 1;
   // and so forth
}

And then you can use them in your code by accessing them like Error::NOT_FOUND and a switch statement wouldn't need to compare strings but has plain ints without downgrading readability.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, that was my other option. But since the codes are strings, not numbers, and PHP anyway can handle strings in switch-case statements or as indices to arrays, is there a point mapping them to integers like that?
@PapaFreud No, of course you can choose what ever you want the constants to be.
2

It really depends on what you want to do with the strings. Do you want to output error messages? Then instead of a case statement you could use a lookup table like this:

$messages = array(
  'NOT_FOUND' => 'The file was not found',
  'EXPIRED' => 'The cookie expired'
  // ETC
);
echo empty($messages[$error]) ? "Unknown error" : $messages[$error];

With PHP 5.3 you could also store code in the array to handle the error situations:

$handlers = array(
  'NOT_FOUND' => function() { /* Error handling code here */ },
  'EXPIRED' => function() { /* Other error handling code */ }
 );
 if(!empty($handlers[$error])) { 
   $handler = $handlers[$error];
   $handler();
 }
 else {
   echo "Could not handle error!"; die();
 }

With a technique like this you avoid case statements that go over several pages.

With PHP < 5.3 you might look into call_user_func for dynamic dispatching of error handling functions.

Comments

0

Strings can't be recognised as class constants. But the answer on your question is that it doesnt really matter if you do it like this:

switch (className::{$errorCode}) { // $errorCode == name of the constant, like NOT_FOUND
// Cases here.
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.