What situations demand the use of eval() because there are no alternatives?

后端 未结 5 1667
小蘑菇
小蘑菇 2021-01-18 00:07

I know eval should be avoided in JavaScript for speed and security reasons. But in the case of PHP, rarely is security ever mentioned. More often, it\'s your pr

5条回答
  •  被撕碎了的回忆
    2021-01-18 00:17

    i completely agree with previous answers in the point that eval is evil, and i never use it in my code.

    but in one situation i haven't managed to avoid eval. i have not much experience in php, so i will be glad if someone advises me how can i rewrite the code without 'eval'. it that situation i had a class name stored in a variable, and i had to call a static method on that class, classname came from a trusted source. so i had to write something like this:

    eval("\$result = $className::methodName()");
    

    (because you cannot just write $className::methodName() );

提交回复
热议问题