0

How to check, if a line of code, that is actually being executed runs from inside of eval() function? Did anyone have to deal with something like this?

5
  • 1
    You could override the default eval function, see docs here php.net/manual/en/function.override-function.php Commented Jul 15, 2016 at 16:04
  • var_dump() works perfectly fine in eval(). Problem is that I have to distinguish, if a library is being executed from inside an eval() while running. I've tried the global variable solution, but inside eval() you've got an access to the same variables as from outside. Commented Jul 16, 2016 at 15:57
  • 1
    worse case: looking through the the stack trace to see where the eval was called from? xdebug has some useful functions for this - I was using them just the other day. You have xdebug active? Commented Jul 16, 2016 at 16:52
  • XDebug is not an option. But I didn't consider the stack trace, seems like a reasonable approach. Commented Jul 16, 2016 at 17:39
  • @RyanVincent Thanks for your input! With your help I found the solution (see accepted answer). Commented Jul 20, 2016 at 9:18

2 Answers 2

1

I'd like to thank Ryan Vincent for his advice!

function is_eval() { $debug = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); foreach ($debug as $step => $trace) { // ignore class calls if (isset($trace['class'])) { continue; } if (isset($trace['function']) && 'eval' === $trace['function']) { return true; } } }

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

1 Comment

How does it work? When should I call is_eval function? It's not very clear to me.
0

Put this in your eval function.

debug_to_console( "Code ran" );

Comments

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.