duankun9280 2011-07-16 18:22
浏览 74
已采纳

如何在PHP的eval()的多次调用之间传递数据?

I'm writing a Wordpress plugin that needs to tailor part of its output to be compatible with a page caching plugin (W3 Total Cache). The technique used by the caching plugin to cache most of the page and only execute fragments is to eval() such code fragments.

However, as my plugin may be invoked in several places within a Wordpress template, I need to keep state between these different instances of my code being eval()-executed. Basically I need the first piece of code to save data that the other code fragments will then use.

Essentially I need the code in the eval() to access data outside of the eval().

Are there any methods of doing this?

EDIT: I should probably add that each execution of eval() is also wrapped in a function that is called as a callback from preg_replace_callback(), so any variables set in the eval() would need to persist through that also

  • 写回答

1条回答 默认 最新

  • doufan3958 2011-07-16 19:00
    关注

    eval() does not create a new variable scope:

    <?php
    
    $foo = 'Hello, World!';
    eval('var_dump($foo); $bar = 33;');
    var_dump($bar);
    

    ... prints this:

    string(13) "Hello, World!"
    int(33)
    

    I'd say all your problem is being able to create global variables from within a function. You can use the usual approaches: global keyword or $GLOBALS array.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛