The makers of Rogaine are making a fortune off me, right now. ![]()
I have a function that I’m creating for parsing chat commands in the chat script I’m developing. There are three different “modes” for these commands: Javascript, PHP, or both. The parsing of the commands depends on the “mode”, or “framework” of said command. I want to handle the selection of parsing methods through a switch statement, and I don’t want to double-up on the coding. To explain better, here’s the function so far:
function parseCommands($msg) {
global $lastFunction;
$lastFunction .= "_parseCommands";
$commandList = getCommands();
foreach ($commandList as $cmd) {
if (false === strpos($msg, $cmd)) continue;
$sql = "select command, framework, adminOnly from chat_commands where symbol = '$cmd';";
$cmdData = getDB($sql);
$command = $cmdData['command'];
$framework = $cmdData['framework'];
$adminOnly = $cmdData['adminOnly'];
switch ($framework) {
case "js":
// parse command for javascript here
break;
case "php":
// parse command for PHP here
break;
case "both":
// parse command for javascript and php combined here here
break;
default:
// you should never get here
}
}
return $msg;
}
In the instance above, you have a “doubling up” of code between the different sections. I’ve thought of just creating separate functions to parse the commands (e.g. parseJs() and parsePHP()), and just calling the seperate functions within the switch statement, but I’m hoping to find an even more efficient way to do this. Is this the best I can hope for? Or is there a better way?