This is actually comes from a specific WordPress issue but there’s a more general PHP point behind it which I’m interested to know the answer to.
The WordPress class is along these lines:
class Tribe_Image_Widget extends WP_Widget {
function example_function(){
// do something useful
}
}
Is there any way in PHP that I can replace example_function()
from outside the class?
The reason I want to do this is that the class is from someone else’s WP plugin (and has several functions in the class) and I want to continue receiving updates to the plugin but I want one of the functions adapted. If I change the plugin, then I’ll lose all my own changes each time. SO if I have my own plugin which just changes that one function, I avoid the problem.
It sounds like what you want to do is
extend
the class, and override that particular function.You could probably have an include_once to the target plugin at the top of your file and then extend the class of the target plugin instead of the WP_Widget class:
As long as the method in the existing class hasn’t been marked as final, you can just subclass and override it