views:

74

answers:

7

Could I achieve this in a more cleaner way? Would be really appricated.

    switch($vocation) {

     case 1: $vocation = "Sorcerer";        break;
  case 2: $vocation = "Druid";           break;  
  case 3: $vocation = "Paladin";         break;   
  case 4: $vocation = "Knight";          break;   
  case 5: $vocation = "Master Sorcerer"; break;   
  case 6: $vocation = "Elder Druid";     break;   
  case 7: $vocation = "Royal Paladin";   break;   
  case 8: $vocation = "Elite Knight";    break;

 }

Thanks!

and sorry about the title couldnt figure out a better one

A: 

Well, depending on what you're using this for (not really sure as there is no context) one option is to have your vocations available in a configuration file (be it XML, text file, etc) that can be read in by the system on the fly. This way, if you ever want to add an additional vocation, you can just update the text file and you don't need to rebuild and/or redeploy your source.

JasCav
+10  A: 

You can use an array instead of the switch statement.

$vocations = array("Sorcerer", "Druid" ...);
$vocationStr = $vocations[$vocation - 1];

Or

$vocations = array(1 => "Sorcerer", 2 => "Druid" ...);
$vocationStr = $vocations[$vocation];

Personally, I am against the reuse of $vocation to refer to two types (integers and String), as they are most likely two different concepts in your program.

I would also advise creating an Enum type for these values instead, but this depends on the context.

notnoop
I like this approach, but I'd probably specify the indexes rather than use the "minus one" thing: array(1=>"Sorcerer", 2=>"Druid",...). This way you're not stuck with the automatic indexes -- what if you decide to eliminate one or skip one later?
JacobM
A: 

An array can work for sequential keys, but use an associative array (hash) otherwise.

%vocations = array("key1"=>"value1", "key2"=>"value2", ...);
$vocationVal = $vocations{$vocationKey};

EDIT: The above is a perl syntax, the php syntax is below:

$vocations = array(key1 => "value1", key2 => "value2", ...);
$vocationVal = $vocations[$vocationKey];
Joy Dutta
Except for array(...), that's perl.
scribble
Thanks, I added the correction.
Joy Dutta
A: 

Either using an array as already mentioned, or, at the very least, using constatants rather than the plain integers in the switch statement. So something like:

define("SORCERER", 1);
define("DRUID", 2);
...
define("RPALADIN", 7);
define("EKNIGHT", 8);

switch($vocation) {
    case SORCERER: $vocation="Sorcerer"; break;
    case DRUID: $vocation="Druid"; break;
    ...
    case RPALADIN: $vocation="Royal Paladin"; break;
    case EKNIGHT: $vocation="Elder Knight"; break;
}

That way adding new classes and ranks between classes is a little easier.

Steerpike
A: 

This gives a trap for initial values of vocation not found in your list.

$vocations = array(1=>"Sorcerer", 2=>"Druid", ...);
if (array_key_exists($vocation, $vocations)) {
  $vocation = $vocations[$vocation];
} else {
  $vocation = 'Not found';
}
Colonel Sponsz
A: 

Another option that i haven't seen here is to keep the data in a table. It will help if the list becomes huge.

vocation_id        vocation_name
     1               sorcerer
     2                druid
    etc....

select vocation_name form vocations where vocation_id=3
Galen