[wp-hackers] function_exists - user woes

garett at harnish.ca garett at harnish.ca
Sat Jul 31 00:16:26 UTC 2004


On Fri, Jul 30, 2004 at 07:08:40PM -0400, Stephen O'Connor wrote:
> Garrett, I like your solution, but why not just keep things short and 
> sweet with wp_tag()? wp_call_plugin_function() sounds too wordy for me, 

My main concern would be confusion of exactly what wp_tag is doing, but
anything like that would work.

> but I realize that my argument pretty much boils down to taste and 
> preference. Also I think semi-colons are more understandable as 
> parameter separators than colons, but ampersands might even be better... :)

LOL.  Whatever, it really doesn't matter to me.  Ampersands might be the
best idea though, keeping with a web parameter idea.

> Also remember to make the second parameter optional:

Yah.  The code was very rough, it was a "how about this" suggestion.  It
wouldn't have been anything thrown into production.

> function wp_tag( $function_name, $parameters = NULL ) {
>  if( function_exists( $function_name ) ) {
>    if( ! is_null( $parameters ) ) {
>      $parameters = split( "[^\\];", $parameters );
>      $parameter_array = array();
>      foreach( $parameters as $parameter ) {
>        $parameter = explode( "=", $parameter, 1 );
>        $parameter_array[ $parameter[0] ] = $parameter[1];
>      }
>      call_user_function( $function_name, $parameter_array );
>    } else {
>      call_user_function( $function_name );
>    }
>  } else {
>    wp_log("Error", "Function $function_name does not exist.");
>  }
> }
> 
> As you can see, I'm still a big fan of creating a WordPress error log. I 
> don't want to be checking my source code for wordpress-published 
> comments (I do that enough by myself already).

Looks good to me.

> Another idea to throw into the pot: what about using func_get_args as 
> well as a clearly defined parameter nomenclature to determine if 
> data-types should be converted from strings? Say if a parameter is 
> called $int_myAge, then the parameter would be converted to an int 
> before being sent to the function.

Hmmm.  Depends, what versions of PHP does wordpress have to support?  So
far we haven't strayed too far from PHP 3.0.3 supported functions.  If we
can use PHP4 functions, then this is an even better idea:

<?php wp_tag ( 'xyz_photos', 'bar', 3 ); ?> 

Yah, that would be a lot nicer to the end user.  How's this:

function wp_tag () {
  if( func_num_args() > 0 ) {
    $parameters = func_get_args();
    $function_name = array_shift( $parameters );

    if( function_exists( $function_name ) )  {
	   if( count( $parameters ) > 0 ) {
	     call_user_func( $function_name, $parameters );
	   } else {
	     call_user_func( $function_name );
	   }
	}
  }
}

Then the actual plugin function would just pull its values out of the
array.  Optimally, though, it would be nice to send each value as it's own
parameter and process it the same (using func_num_args and such), but that 
would be quite difficult, I think.

--
Garett
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : /pipermail/hackers_wordpress.org/attachments/20040730/e174b072/attachment.bin


More information about the hackers mailing list