The best method working with USSD
is making each command
a separate class. This way you can work , debug and maintain a large USSD
application
Example
$ussd = new USSDMessage(); // USSD Class
$main = new Command("menu"); // Starting Command
// Register multiple command
$main->register(new Command("help"));
$main->register(new Command("account"));
$main->register(new Command("transfer"));
$main->register( new Recharge("reacharge"));
$main->run($ussd); // Run Request
Basic Class Structure
// Class To receve message from USSD
class USSDMessage {
function next();
function saveSession();
}
// Basic Command Class
class Command {
private $name;
private $cmd = array();
function __construct($name) {
$this->name = $name;
}
function register(Command $menu) {
$this->cmd[$menu->getName] = $menu;
}
function run(USSDMessage $ussd) {
$key = $ussd->next(); // get next inpur from ussd
if (isset($this->cmd[$key])){
$ussd->run($ussd);
}
}
}
// Recharge Command Example
class Recharge extends Command {
function run(USSDMessage $ussd) {
$key = $ussd->next();
switch ($key){
case "1":
// Proccess card recharge
// Do Somthing
$ussd->saveSession(); // End Session
break;
case "2":
// Process direct deposit
// Do Somthing
$ussd->saveSession(); // End Session
break;
default:
// Format Not supported
// Do Somthing
$ussd->saveSession(); // End Session
break;
}
}
}