[wp-hackers] function_exists - user woes

Stephen O'Connor steve at stevarino.com
Fri Jul 30 23:08:40 UTC 2004


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

Also remember to make the second parameter optional:

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

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.

- Stephen

Garett Harnish wrote:

> Chris Coggburn wrote:
>
>> Garett Harnish wrote:
>>
>>> That sounds like a very easy to implement and functional solution.  
>>> How about something like this:
>>>
>>> <snip>
>>>
>>> <?php wp_call_plugin_function('xyz_photos', array('foo' => 'bar', 
>>> 'x' => '3')); ?>
>>
>>
>> That looks good to us, but remember people that know nothing about 
>> php will be completely confused about that. Sure they could just copy 
>> and paste things...but it makes the user feel a little happier when 
>> they actually understand what is going on in what they slap in their 
>> template.
>
>
>
> True.  The reason for the array is mostly to reduce the code of 
> wp_call_plugin_function.   The problem is the string "foo=bar:x=3" is 
> a lot more complicated to do things with.   Still, it is doable:
>
> function wp_call_plugin_function ($function, $str_params)
> {
>  // see if function exists
>  if (function_exists($function))
>  {
>         // Break string into key/value pairs
>         $array_temp = split(":", $str_params);
>
>         // Build Associative Array
>         foreach ($array_temp as $pair)
>        {
>                 list($key,$value) = split("=", $pair, 2);
>                 $array_params[$key] = $value;
>         }
>
>         // Call function
>         call_user_func($function, $array_params);
>  }
> }
>
> Then one could call the function like so:
> <?php wp_call_plugin_function('xyz_photos','foo=bar:x=3'); ?>
>
> Assuming there isn't a logic flaw in my code, that should do it ... 
> but it lacks elegance.  Still, it should make it easier for an 
> end-user to understand what they are doing when they plug in the 
> function call.
>
>>> Using a parameter array would definitely make it a lot easier as well.
>>
>>
>>
>> This would, but would confuse the user as said before.
>
>
>
> Would the above be better?
>
> -- 
> Garett
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>hackers mailing list
>hackers at wordpress.org
>http://wordpress.org/mailman/listinfo/hackers_wordpress.org
>




More information about the hackers mailing list