0

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.

Luis Enrique
  • 440
  • 1
  • 4
  • 10
  • You need to assign something to `$$a` , it doesn't exist. – AbraCadaver Jun 10 '15 at 17:55
  • 1
    @Luis Enrique, I don't think you'll find a satisfactory answer over at the question pointed to by the duplicate notice. In this case it looks like `$MSG78`, `$ICONDEPOSIT`, etc are defined. And you are converting the strings to variables correctly (`$$a`) but all those variables are out of scope for your callback function to array map. See http://php.net/manual/en/functions.anonymous.php, specifically "Example #3 Inheriting variables from the parent scope." – dblue Jun 10 '15 at 18:07

0 Answers0