Tuesday, 15 February 2011

wordpress - redeclare a php-function -



wordpress - redeclare a php-function -

a normal installation of wordpress cms consists of hundreds of scripts, plugins , themes. concept allows replace scripts creating re-create of , set altered version in right directory. however, copied script not updated when new version of script or plugin installed. within script there hundreds of functions. cost of changing 1 function other functions no longer updated. in many cases wordpress hooks , filters sufficient add together or suppress specific functionality, not always.

i'm looking way redeclare or overload function original script may replaced cms whenever update installed. tried this:

rename_function( 'wp_function' , 'old_wp_funtion ) function wp_function(){ // things before function $a = $b; // phone call old function $result = old_wp_function(); // things afterwards $b = $c; homecoming $result; }

but doesn't work. also, wordpress never know kind of , how many arguments aspect. overloaded function has deal too.

my question is: how go create work in cases, number , type of arguments. , without having alter original function/script because may (and must) overwritten wordpress updates.

php not standard back upwards redefining of functions 1 time they're declared. there number of ways forcefulness it, in general it's considered very poor programming practice, , discouraged.

if want overload code in php, much improve off writing code in classes rather plain functions, because class can extended, , methods overloaded. by far best alternative you. may require bit of reworking of code, it's practice write code in classes anyway, consider chance improve code quality.

another alternative utilize namespaces. php 5.3 , later supports namespaces, means can declare same function name multiple times, in different namespaces. if code isn't using namespaces involve important amount of work implement, may help allow want. suspect class alternative mentioned improve bet.

with specific reference rename_function() function you're trying utilize here: part of a non-standard debugger extension called adp; it's developer's debugger, shouldn't used in production system.

if you're determined way want to, other options seek include runkit extension, explicitly designed allow kind of think you're asking about. again, it's intended developer utilize only, , discouraged production use.

another alternative patchwork library, pure php code (ie doesn't require php extension installed). makes much more appealing solution. again, author explicitly recommends against using other development environment.

so there have it: several options exist you. if must proceed downwards route you've described, suggest using patchwork library.

but 1 time again you're asking not thought -- big clue should no-one else doing way, , doing similar things saying @ every chance shouldn't used in production.

the case it's considered useful tool writing test scripts, have dependencies can't avoided. redefining dependencies mocks or stubs can create possible write unit tests otherwise untestable code.

but aside that, consistent message php world is: don't redefine functions.

php wordpress overloading redeclare

No comments:

Post a Comment