I'm currently learning PHP for a new project.... This project is supposed to be all configurable hence, it was decided to use strings as our data type for configuration. I have created the following MVP:
function decodifyAppLauncher($strAppLauncher)
{
$strAppLauncher= "BTN_DEPOSIT|BTN_DISPENSE|BTN_CHANGE";
$BTN_DEPOSIT = "MSG78|ICONDEPOSIT|DEPOSIT";
$BTN_DISPENSE = "MSG78|ICONDEPOSIT|DEPOSIT";
$BTN_CHANGE = "MSG78|ICONDEPOSIT|DEPOSIT";
$MSG78 = "ESTE ES UN MENSAJE";
$ICONDEPOSIT = "ESTE ES UNA IMAGEN";
$DEPOSIT = "ESTE ES UN PATH";
$vMenu = array();
$vElementsDecodified = array();
// Read from AppLauncher and configure Menu
$vAppLauncher = explode("|",$strAppLauncher);
foreach ($vAppLauncher as $key => $strCurrentItem){
// Decodify button information, MSG, ICON, PATH1 ... PATHN (CHANGES STRINGS TO VARIABLES)
$vValues = array_map(function($a){ return $$a; }, explode("|", $$strCurrentItem));
// Append our list-values to our menu-list, but with values decodified
$vMenu[] = $vValues;
}
return $vMenu;
}
And this gives the following errors:
Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: MSG78 in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: ICONDEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
Notice: Undefined variable: DEPOSIT in C:\USR\CRLink\www\cashbankmanagement\functions.php on line 21
I know I'm messing up converting string to variable names, what would be the best approach to this problem?
How should I do the array_map ?
Thanks!!!
Edit1:
Personally I don't think it is a repeated question... I believe @dblue is right I'm messing variable scope in this line:
$vValues = array_map(function($a){ return $$a; }, explode("|", $$strCurrentItem));
I should change it as:
$vValues = array_map(function($a) use ($$a) { return $$a; }, explode("|", $$strCurrentItem));
Or use a constant or global variable, I can't test it right now but I'll test it soon. This question is more about lambda functions in PHP.