是否有可能替换PHP类中的函数? [英] Is it possible to replace a function within a PHP class?
问题描述
这个WordPress类有以下几行:
class Tribe_Image_Widget extends WP_Widget {
function example_function(){
//做一些有用的事情
}
}
PHP中有什么方法可以从类外部替换 example_function()
吗?
我想这样做的原因是该类来自别人的WP插件(并且在该类中有几个函数),并且我想继续接收插件的更新,但我希望调整其中一个函数。如果我更换插件,那么每次都会丢失所有我自己的更改。所以如果我有我自己的插件,只是改变了这个功能,我就避免了这个问题。
这听起来像你想要的东西要做的是扩展
类,并覆盖该特定函数。 类Your_Tribe_Image_Widget扩展Tribe_Image_Widget
{
函数example_function(){
//调用基类的功能
//如果需要的话...
parent :: example_function() ;
//做其他有用的
}
}
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.
Class Your_Tribe_Image_Widget extends Tribe_Image_Widget
{
function example_function() {
// Call the base class functionality
// If necessary...
parent::example_function();
// Do something else useful
}
}
这篇关于是否有可能替换PHP类中的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!