PHP - override existing function [duplicate]

2019-01-14 22:13发布

This question already has an answer here:

Can I redeclare a existing function, with the same name, but different code? Or somehow "disable" the old function?

I want to redefince a core WordPress function, but since plugins and theme call this function a lot, I need to keep the same function name.

5条回答
聊天终结者
2楼-- · 2019-01-14 22:23

You could use the WordPress hooks (called filters and actions) and then use the add_filter() function to override the function you are using.

i.e.

function function_name() {
 //code goes here
}

$hook = 'get_options'; // the function name you're filtering
add_filter( $hook, 'function_name' );

The Codex will help a lot with this.

http://codex.wordpress.org/Plugin_API/Filter_Reference

查看更多
萌系小妹纸
3楼-- · 2019-01-14 22:23

You can wrap the block defining the original function in a conditional checking if another of the same name is not already defined (I'm assuming you mean Wordpress functions and not core PHP ones)

    <?php 
if(!function_exists('function_name')){ 
    //old definition here
    } ?>

You could then redefine it above while still preserving the original should you need to roll back to it.

Depending on how complex the changes are and how many times you may do this, you may also want to look into Namespaces if you are on PHP 5.

查看更多
够拽才男人
4楼-- · 2019-01-14 22:27

Just comment the old function out and write your new one ;)

In PHP it is not possible to redefine or overload (i.e. define a function with the same name but different parameters) a function natively. There though are extensions like runkit which allow to redefine functions (runkit_function_redefine), but you probably don't want to use these (such extensions are rarely installed and mostly unreliable.)

查看更多
小情绪 Triste *
5楼-- · 2019-01-14 22:39

My attempted solution was to do this:

function suffusion_get_image($options = array()) {
    include_once ABSPATH.'wp-content/themes/suffusion-child/functions/media.php';
    return childtheme_overide_suffusion_get_image($options = array());
    ....
}

Obviously there is an overhead at upgrade as you would need to add lines back into the scripts again and I have used this method successfully to date but now trying to do it with get_terms in the wp-includes and hitting a redeclaration issue which I am trying to resolve or workaround at the moment.

My reason to edit core is that the existing core does not provide in a convenient way for a multisite requirement.

Someone has just suggested on another forum however using override_function but the manual is worded such that it appears to be of use only to built-in functions - I took it that means PHP built in functions

查看更多
Animai°情兽
6楼-- · 2019-01-14 22:42

Only if you use something like APD that extends the zend engine to allow for that:

Intro

Override Method Docs

Note: Runkit seems like a better option than APD since it's more specific to this purpose and would allow you to keep the original method intact at a different address.

查看更多
登录 后发表回答