2

This code works but $vars can't be defined in the call() function. Why do $vars can't be passed to the array_walk_recursive()?

class lib{

    private $library;

    function __construct($lib="")
    {
        $this->library = $lib;
    }
    function set($vars)
    {
        $decoded_classes = json_decode($this->library,true);
        array_walk_recursive($decoded_classes,function(&$f) {$f = create_function($vars,$f);});
        return $decoded_classes;
    }
}
$json = '
{     
    "class1": {     
        "function1":"return \"$a<b>$b</b>!\";"
    },
    "class2": {     
        "function2":"return $b;",
        "function3":"return $c;"
    },
    "function1":"return \"test\";"
}';
$lib = new lib($json);
$lib = $lib->set("$a,$b");
$lib = $lib["class1"]["function1"]("asdasasd","asdasasd");
echo $lib;

1 Answer 1

6

Firstly, have a look at this example with variable scoping for closures. You need to pass the variable in with the use keyword, eg:

array_walk_recursive($decoded_classes,function(&$f) use ($vars) {$f = create_function($vars,$f);});

It would be good if you defined $a, $b, etc for us, so we could actually test your code.

Sign up to request clarification or add additional context in comments.

1 Comment

This answer was of limited help for the OP here, the code just went into an additional question: PHP, Parse error: syntax error, unexpected T_FUNCTION. Users finding it here might be also interested in learning about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.