dongmi1941 2010-07-06 17:38
浏览 132

防止PHP require_once永远运行

Debugging someone else's PHP code, I'd like to selectively override one of their classes. The class is included via:

require_once('classname.php');

But, that appears in various places in the application. I'd rather 'simulate' the require_once, so that it never gets run at all. I.e. just define class classname as I want it. Then, the next time the file was require_once'ed, it'd be flagged as already-loaded and thus not reloaded.

I can create a classname.php file of my own, but I'd rather keep the testing I'm doing contained to a single file, as I'm doing this possibly for many classes, and I'd like easier control over the overriding.

In Perl, what I'd want to do would be:

$INC{'classname.pm'} = 1;

Is there a way to access PHP's equivalent of Perl's %INC?

Update: Consistently surprised by what PHP doesn't let you do...

My workaround was to use runkit's runkit_method_redefine. I load the classes I was trying to prevent loading, and then redefine all the methods I was trying to 'mock', e.g.:

require_once('classname.php');
runkit_method_redefine('classname','method','$params','return "testdata";');
  • 写回答

3条回答 默认 最新

  • drq22639 2010-07-06 18:01
    关注

    Place:

    <?php return; ?>
    

    at the very top of classname.php file.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器