views:

178

answers:

1

Being new to PHP, and with a lot riding on this "function wrapper", I thought I'd get a few opinions and a little feedback. I'd like to get about five comments, if possible.

Now before you ask, I have many reasons for wanting to wrap other (WordPress) functions, the primary being hassle-free upgrading. It was also important for me to be able to set a custom name for each function definition, hence the $wrap array.

But I digress, does this look acceptable and relatively bulletproof?

function core_oo( $function )
{
    $args = array_slice( func_get_args(), 1 );
    $wrap = array
    (
        'comment' => 'the_comment',
        'comments' => 'have_comments',
        'post' => 'the_post',
        'posts' => 'have_posts'
    );
    return call_user_func_array( $wrap[ $function ], $args );
}

... and the function will be called like...

core_oo( 'post', 'arg1', 'arg2' );

Many thanks!

EDIT:

Per chaos's sugeestion below, is this the right way to declare $wrap as static?

static $wrap = array
( ...
+6  A: 

Well, your fundamental aim seems like madness, but with your fundamental aim taken as a given, yes, that function looks like a fine way to accomplish it.

You should declare $wrap as static to ensure that you're not pointlessly regenerating the array every time the function is called, though.

And this:

if(!isset($wrap[$function]))
    trigger_error('No underlying function known for ' . $function, E_USER_ERROR);

would probably be smart too.

chaos
Thanks chaos! I've heard from others suggesting that my fundamental aim seems weird, but never a good reason. My (possibly flawed) thought was that during a WordPress upgrade, if something in my theme becomes broken, I'll have an easy reference of what to check to have it fixed in a jiffy.Furthermore, by wrapping all these WP functions, if something becomes broken during an upgrade, it'll be a matter of changing one line instead of many.Do you know of a better way to accomplish this?
Jeff
Honestly, I don't really see how this wrapper is going to make any of the things you describe easier than just using the native WP calls would. Which is why it seems weird.
chaos
I am using about 40 default WordPress functions, which are sprinkled throughout my theme files. I thought by wrapping them, and using them from one central location, if one function becomes obsolete or broken... better to change one line in one file than many lines in many files.
Jeff
Fair enough. If that happens frequently across WP upgrades, and you can usually fix it by calling a different function with the same arguments, then I suppose this would be productive.
chaos
Many thanks! I am officially declaring the stack overflow community the best on the web. :)
Jeff