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 run开发者_C百科kit_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";');
Place:
<?php return; ?>
at the very top of classname.php
file.
Then, the next time the file was require_once'ed, it'd be flagged as already-loaded and thus not reloaded.
require_once does this for you..
Additionally you could use something hacky like
if(!class_exists("my_class")) {
class my_class {} // define your class here
}
I'm thinking what you mean is that it's entirely possible that there is another "required_once" somewhere else in the code that would cause yours to be overwritten.
There are two things you can do:
1) Hunt down the first "required" and then comment it out.
2) Go into the class file and include your class and return before the other class is defined.
精彩评论