Redefine Class Methods or Class

It’s called monkey patching. But, PHP doesn’t have native support for it.

Though, as others have also pointed out, the runkit library is available for adding support to the language and is the successor to classkit. And, though it seemed to have been abandoned by its creator (having stated that it wasn’t compatible with PHP 5.2 and later), the project does now appear to have a new home and maintainer.

I still can’t say I’m a fan of its approach. Making modifications by evaluating strings of code has always seemed to me to be potentially hazardous and difficult to debug.

Still, runkit_method_redefine appears to be what you’re looking for, and an example of its use can be found in /tests/runkit_method_redefine.phpt in the repository:

runkit_method_redefine('third_party_library', 'buggy_function', '',
    'return \'good result\''
);

Leave a Comment