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";');