Corveda / PHPSandbox

A PHP-based sandboxing library with a full suite of configuration and validation options.
https://phpsandbox.org
Other
220 stars 46 forks source link

Carrying a context through multiple executions #7

Closed datashaman closed 8 years ago

datashaman commented 8 years ago

I was able to get my idea working with plain eval. See the code below:

function staticEval($_code)
{
    static $_vars = [];
    extract($_vars);
    $_result = eval($_code);
    $_vars = get_defined_vars();
    unset($_vars['_vars']);
    unset($_vars['_code']);
    unset($_vars['_result']);
    return $_result;
}

staticEval('$a = "hello world!";');
staticEval('echo "$a\n";');
fieryprophet commented 8 years ago

Neat!