Plugin / Safe Function Call

Scott Reilly

Description

Description

Safely call a function, class method, or object method in a manner that doesn’t generate errors if those plugins cease to exist.

Various helper functions are provided that provide handy variations of this theme:

  • _sfc(): Safely call a function and get its return value
  • _sfce(): Safely call a function and echo its return value
  • _sfcf(): Safely call a function; if it doesn’t exist, then a fallback function (if specified) is called
  • _sfcm(): Safely call a function; if it doesn’t exist, then echo a message (if provided)

Let’s assume you had something like this in a template:

<?php list_cities( 'Texas', 3 ); ?>

If you deactivated the plugin that provided list_cities(), your site would generate an error when that template is accessed.

You can instead use _sfc(), which is provided by this plugin to call other functions, like so:

<?php _sfc( 'list_cities', 'Texas', 3 ); ?>

That will simply do nothing if the list_cities() function is not available.

If you’d rather display a message when the function does not exist, use _sfcm() instead, like so:

<?php _sfcm( 'list_cities', 'The cities listing is temporarily disabled.', 'Texas', 3 ); ?>

In this case, if list_cities() is not available, the text “The cities listing is temporarily disabled.” will be displayed.

If you’d rather call another function when the function does not exist, use _sfcf() instead, like so:

<?php
    function unavailable_function_handler( $function_name ) { echo "The function $function_name is not available."; }
    _sfcf( 'nonexistent_function', 'unavailable_function_handler' );
?>

In the event you want to safely call a function and echo its value, you can use _sfce() like so:

<?php _sfce( 'largest_city', 'Tx' ); ?>

Which is roughly equivalent to doing :

<?php if function_exists( 'largest_city' ) { echo largest_city( 'Tx' ); } ?>

Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage

Template Tags

The plugin provides four functions for your use. Note: These functions are not limited to use in templates

Functions

  • <?php function _sfc($callback) ?>
    This will safely invoke the specified callback. You can specify an arbitrary number of additional arguments that will get passed to it. If the callback does not exist, nothing is displayed and no error is generated.

  • <?php function _sfce($callback) ?>
    The same as _sfc() except that it echoes the return value of the callback before returning that value.

  • <?php function _sfcf($callback, $fallback_callback = '') ?>
    The same as _sfc() except that it invokes the fallback callback (if it exists) if the callback does not exist. $function_name_if_missing() is sent $function_name as its first argument, and then subsequently all arguments that would have otherwise been sent to $function_name().

  • <?php function _sfcm($callback, $message_if_missing = '') ?>
    The same as _sfc() except that it displays a message (the value of $message_if_missing), if the callback does not exist.

Arguments

  • $callback
    A string representing the name of the function to be called, or an array of a class or object and its method (as can be done for add_action()/add_filter())

  • $message_if_missing
    (For _sfcm() only.) The message to be displayed if $function_name() does not exist as a function.

  • $fallback_callback
    (For _sfcf() only.) The function to be called if the callback does not exist.

Examples

  • <?php _sfc('list_cities', 'Texas', 3); /* Assuming list_cities() is a valid function */ ?>
    “Austin, Dallas, Fort Worth”

  • <?php _sfc(array('Cities', 'list_cities'), 'Texas', 3); /* Assuming list_cities() is a valid function in the 'Cities' class */ ?>
    “Austin, Dallas, Fort Worth”

  • <?php _sfc(array($obj, 'list_cities'), 'Texas', 3); /* Assuming list_cities() is a valid function in the object $obj */ ?>
    “Austin, Dallas, Fort Worth”

  • <?php _sfc('list_cities', 'Texas', 3); /* Assuming list_cities() is not a valid function */ ?>
    “”

  • <?php _sfcm('list_cities', 'Texas', 'Unable to list cities at the moment', 3); /* Assuming list_cities() is a valid function */ ?>
    “Austin, Dallas, Fort Worth”

  • <?php _sfcm('list_cities', 'Texas', 'Unable to list cities at the moment', 3); /* Assuming list_cities() is not a valid function */ ?>
    “Unable to list cities at the moment”

  • <?php _sfce('largest_city', 'Tx'); /* Assuming largest_city() is a valid function that does not echo/display its return value */ ?>
    “Houston”

  • <?php
    function unavailable_function_handler( $callback ) {
    echo "Sorry, but the function {$callback}() does not exist.";
    }
    _sfcf('nonexistent_function', 'unavailable_function_handler');
    ?>

Ratings

5
1 reviews

Rating breakdown

Details Information

Version

1.2.8

First Released

08 Jun, 2009

Total Downloads

2,273

Wordpress Version

1.5 or higher

Tested up to:

5.2.4

Require PHP Version:

-

Tags

Contributors

Languages

The plugin hasn't been transalated in any language other than English.

DIRECTORY DISCLAIMER

The information provided in this THEME/PLUGIN DIRECTORY is made available for information purposes only, and intended to serve as a resource to enable visitors to select a relevant theme or plugin. wpSocket gives no warranty of any kind, express or implied with regard to the information, including without limitation any warranty that the particular theme or plugin that you select is qualified on your situation.

The information in the individual theme or plugin displayed in the Directory is provided by the owners and contributors themselves. wpSocket gives no warranty as to the accuracy of the information and will not be liable to you for any loss or damage suffered by you as a consequence of your reliance on the information.

Links to respective sites are offered to assist in accessing additional information. The links may be outdated or broken. Connect to outside sites at your own risk. The Theme/Plugin Directory does not endorse the content or accuracy of any listing or external website.

While information is made available, no guarantee is given that the details provided are correct, complete or up-to-date.

wpSocket is not related to the theme or plugin, and also not responsible and expressly disclaims all liability for, damages of any kind, arising out of the use, reference to, or reliance on, any information or business listed throughout our site.

Keep Leading Your Followers!
Share it for them.