1

A normal installation of the WordPress CMS consists of hundreds of scripts, plugins and themes. The concept allows to replace scripts by simply creating a copy of it and put the altered version in the right directory. However, a copied script will not be updated when a new version of the script or plugin is installed. Within a script there are often hundreds of functions. The cost of changing one function is that the other functions will also be no longer updated. In many cases the Wordpress hooks and filters are sufficient to add or suppress specific functionality, but not always.

I'm looking for a way to redeclare or overload a function so that the original script may be replaced by the CMS whenever an update is installed. I tried this:

rename_function( 'wp_function' , 'old_wp_funtion )

function wp_function(){
        // things to do before the function
    $a = $b;
        // call the old function
    $result = old_wp_function();
        // things to do afterwards
    $b = $c;

    return $result;
}

But it doesn't work. Also, with Wordpress you never know what kind of and how many arguments to aspect. The overloaded function has to deal with that too.

My question is: how do I go about this to make it work in all cases, with any number and any type of arguments. And without having to change anything to the original function/script because they may (and must) be overwritten by Wordpress updates.

3
  • I don't know any doesn't work error in php or in any other language. BTW, you have typo (closing ' missing after 'old_wp_funtion).
    – Leri
    Feb 21, 2013 at 10:08
  • And also php supports namespaces that are common way to handle situation when names of functions are ambiguous.
    – Leri
    Feb 21, 2013 at 10:09
  • I think you need to delve into more detail as to what you are trying to achieve with this. Whatever it is this sounds like it will be the wrong approach.
    – Dunhamzzz
    Feb 21, 2013 at 10:24

1 Answer 1

1

PHP does not as standard support redefining of functions once they're declared. There are a number of ways to force it, but in general it's considered very very poor programming practice, and strongly discouraged.

If you really want to overload code in PHP, you would be much better off writing the code in classes rather than plain functions, because a class can be easily extended, and methods overloaded. This would be by far the best option for you. It may require a bit of reworking of your code, but it's good practice to write code in classes anyway, so consider it a chance to improve your code quality.

Another option is to use namespaces. PHP 5.3 and later supports namespaces, which means that you can declare the same function name multiple times, but in different namespaces. If your code isn't currently using namespaces this could involve a fairly significant amount of work to implement, but it may help allow you to do what you want. But I suspect that the class option I mentioned previously would be a better bet.

With specific reference to the rename_function() function that you're trying to use here: It is part of a non-standard debugger extension called ADP; it's a developer's debugger, so certainly shouldn't be used in a production system.

If you're determined to do it the way you want to, other options you could try include the Runkit extension, which is explicitly designed to allow exactly the kind of think you're asking about. Again, it's intended for developer use only, and discouraged for production use.

Another option is the Patchwork library, which is pure PHP code (ie it doesn't require a PHP extension to be installed). This makes it a much more appealing solution. But again, the author explicitly recommends against using it for anything other than a development environment.

So there you have it: several options exist for you. If you must proceed down the route you've described, I suggest using the Patchwork library.

But I will say again that what you're asking for is not a good idea -- the big clue should be that no-one else is doing it this way, and those who are doing similar things are saying at every opportunity that it shouldn't be used in production.

The only case where it's considered a useful tool is for writing test scripts, where you have dependencies that can't be avoided. Redefining the dependencies into mocks or stubs can make it possible to write unit tests for otherwise untestable code.

But aside from that, the consistent message from the PHP world is: don't redefine your functions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.