Title: Handle PHP closures please
Last modified: August 24, 2016

---

# Handle PHP closures please

 *  [loushou](https://wordpress.org/support/users/loushou/)
 * (@loushou)
 * [11 years ago](https://wordpress.org/support/topic/handle-php-closures-please/)
 * Hi there. Love the plugin. It does as advertised… mostly. That being said, we
   are using the latest version of the plugin, and we are running into a problem.
 * Our site gets a very high amount of traffic. As such, we have implemented several
   layers of caching to speed up the experience. One of the layers of caching specifically
   caches the output of each widget in memcache. The way we accomplish this is by
   changing the ‘callback’ value of most widgets to a dynamic closure that contains
   all the settings we need in it’s scope. This is really the only way to accomplish
   what we need to, so there is no real leeway here. The problem is that this plugin
   is preventing this method from working in some cases, because it is throwing 
   a fatal error.
 * After some debugging, I found the exact lines that are causing the problem, and
   I have a very workable solution for it that is super easy to implement.
 * **General:** in file `display-widgets/display-widgets.php`, on line 195, you 
   have ternary statement that assumes that all callbacks are going to be either
   an array (in array( $object, $method ) form) or a string (for $function_name 
   form). This is not always correct, especially when you take into consideration
   php 5.3+.
 * **First problem:** array form callbacks are not necessarily going to be in array(
   $object, $method ) form. They could also be in array( $classname, $method ) form.
   When this is the case, the first value of the array is not an object, it is a
   class, so using the ‘object operator’ (->) is not valid on line 195. You would
   instead need to use the ‘paamayim nekudotayim’ (::) operator like this $classname::
   $static_property_name (php 5.3+) or some wrapped function that takes advantage
   of get_class_vars() like the [selected answer shows on this stackoverflow post](http://stackoverflow.com/questions/1279081/getting-static-property-from-a-class-with-dynamic-class-name-in-php#answer-1279124).
   In your case, you are looking for a class variable name, called ->id_base, which
   is not guaranteed to be there, since you are not guaranteed to have an object.
 * **Second problem:** (more specific to my situation) you only check if the callback
   is an array or string. As of 5.3+ you can have a closure, which is an object 
   technically. Thus your next if statement on line 197 will resolve to false (“
   no this is not empty”, cause it is an object), which will cause the next line
   of code on line 201 to cause a fatal error, since you cannot convert an object
   to a string.
 * **Solution:** really you have two options here, the way I see it.
 * **Option 1:** (most preferrable, but more work) use the $w variable here (defined
   on line 185) to obtain the id_base and the widget number:
 * `$pieces = explode( '-', $w );`
    `$number = array_pop( $pieces );` `$id_base 
   = implode( '-', $pieces )`
 * then proceed with your other logic…. or
 * **Option 2:** (less preferrable, and less work) ignore any widgets for which 
   you cannot determine an id_base for, based on your current method. This can be
   accomplished by changing the if statement on line 197 to something like this:
 * `if ( ! $id_base || ! is_string( $id_base ) ) {`
 * where you test the obtained value for being a string in addition to empty, before
   you continue
 * This plugin does do as advertised, for the core WordPress widgets, as well as
   many others. But to be most flexible, you need to take into account that not 
   everyone will follow the same form factor, or use the same methods as the norm.
   In the mean time I have (against my better judgement) edited my own version of
   this plugin until a fix can be implemented on your side.
 * Thanks in advance,
    Loushou
 * [https://wordpress.org/plugins/display-widgets/](https://wordpress.org/plugins/display-widgets/)

Viewing 1 replies (of 1 total)

 *  [Chris Dillon](https://wordpress.org/support/users/cdillon27/)
 * (@cdillon27)
 * [11 years ago](https://wordpress.org/support/topic/handle-php-closures-please/#post-5977476)
 * Nice work, Loushou. I suggest you submit a pull request on [Github](https://github.com/Strategy11/display-widgets)
   to get the author’s attention.

Viewing 1 replies (of 1 total)

The topic ‘Handle PHP closures please’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/display-widgets_f8f8f8.svg)
 * [Display Widgets](https://wordpress.org/plugins/display-widgets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/display-widgets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/display-widgets/)
 * [Active Topics](https://wordpress.org/support/plugin/display-widgets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/display-widgets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/display-widgets/reviews/)

## Tags

 * [callbacks](https://wordpress.org/support/topic-tag/callbacks/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 1 reply
 * 2 participants
 * Last reply from: [Chris Dillon](https://wordpress.org/support/users/cdillon27/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/handle-php-closures-please/#post-5977476)
 * Status: not resolved