function isEval()
{
foreach(array_reverse(debug_backtrace()) as $v) {
return $v['function'] === 'eval';
}
}
function isEval() { // updated by code90
foreach(array_reverse(debug_backtrace()) as $v) {
if($v['function'] === 'eval') return true;
}
return false; }
function myFunc()
{
return isEval() ? "is eval\n" : "is not eval\n";
}
function callAnotherFunction()
{
return myFunc();
}
function myFunctionWithEval()
{
eval('$return = myFunc();');
return $return;
}
测试
echo "myFunc() without eval: " . myFunc();
eval("echo 'myFunc() whit eval: ' . myFunc();");
echo "callAnotherFunction() without eval: " . callAnotherFunction();
eval("echo 'callAnotherFunction() with eval: ' . callAnotherFunction();");
echo 'myFunctionWithEval() with eval: ' . myFunctionWithEval();
输出
myFunc() without eval: is not eval
myFunc() whit eval: is eval
callAnotherFunction() without eval: is not eval
callAnotherFunction() with eval: is eval
myFunctionWithEval() with eval: is not eval <--- PROBLEM!
myFunctionWithEval 使用 eval 调用 myFunc。你不能保证它什么时候被调用 eval。可以给出假阳性。
你应该考虑另一种方法来进行。不应该使用这种方法!
另一种选择,但我始终建议不要使用:
function isEval()
{
return isset($GLOBALS['__MyEval']) && $GLOBALS['__MyEval'] === true;
}
function myFunc()
{
return isEval() ? "is eval\n" : "is not eval\n";
}
function myFunctionWithEval()
{
$GLOBALS['__MyEval'] = true;
eval('$return = myFunc();');
$GLOBALS['__MyEval'] = null;
return $return;
}
echo "myFunctionWithEval() with eval: " . myFunctionWithEval();