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 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型