PHP - override existing function [duplicate] PHP - override existing function [duplicate] wordpress wordpress

PHP - override existing function [duplicate]


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 filteringadd_filter( $hook, 'function_name' );

The Codex will help a lot with this.

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


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.)


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.