Odd behavior with PHP’s method_exists() function

I recently had a report from a user that many of the pages within the wp-SwimTeam plugin were blank.  This is odd and incorrect behavior but I have seen it before.  When I last chased it down I found that the way various versions of PHP 5.2.x and 5.3.x handled Constructors was different and in some versions Constructors in parent classes would be called and in some versions they wouldn’t.

I assumed this new report of blank pages meant that I had missed one of these Constructor issues somewhere.  It turns out that wasn’t the case.  This time I found that PHP’s method_exists() function behaves differently between different versions of PHP on different platforms.

The report came in from a site running PHP 5.3.13 which is the same as what I am running in my development area.  The only difference I can see between the two servers is mine is running on Windows under IIS where as the problematic site is running Apache under Linux.

When a page is rendered for wp-SwimTeam, the objects on the page are traversed and HTML code is generated.  The objects which comprise the page content can either be strings (HTML code) or objects themselves which then traversed recursively until all of the leaf HTML is assembled.  This is core functionality in phpHtmlLib that I have been using for years without any issues until now.

What I found was that if the variable passed to method_exists() was a string instead of an object, PHP goes out in the weeds and never returns which ultimately results in the blank page.  This is the code that has been rendering objects in phpHtmlLib for 10+ years:

if (method_exists($item, "render") ) {

It was fairly simple to fix it.  By checking to see if the item is an object prior to checking for it’s render method, the problem is resolves and the page renders correctly.

if (is_object($item) && method_exists($item, "render") ) {

I will be updating phpHtmlLib with this fix shortly.

4 thoughts on “Odd behavior with PHP’s method_exists() function

  1. I had a few bug reports, I wasn’t sure if this is where to report bugs!
    First off, wp-SwimTeam is one of the most polished and complete WordPress plugins I have ever seen. Keep up the good work!
    Now for the bugs…
    1) When selecting jobs (or a primary contact field) it shows the name as N/A, N/A even though the First and Last name values are populated in the MySQL Database
    2) Parents can register any child, not just ones who they are listed as the primary contact for in meets
    3) Parents can register their children for meets even before the children are set as active for the season
    I am not sure if any of these are by design, I would have fixed it my self but I didn’t know what files did what!
    Once again thanks for a great WordPress plugin!

  2. Mike,

    Couple of issues I am running into.
    – Duplicate team roster
    – Overview page is counting users too. Swimmers should be the only active from roster and assigned agegroup
    – email issue on registration. Using WP-MAIL-SMTP and not working to send registration out to email address. Not sure if I need to reconfigure mercury mail?
    I will send this to your gmail account too.

      • Can you elaborate on the duplicate team roster? Do you see every swimmer listed twice?
      • Does the Overview page reflect the fact that you appear to have a duplicate roster? In my quick test my Overview page correctly matches the roster (which does not have duplicates). There is zero connection between the roster the number of WordPress Users so I am not sure how that could get combined.
      • The plugin simply makes a call to wp_mail() (part of the WordPress API). Are new user registration emails (which are built into WordPress) going out correctly? I use WP-SMTP-Mail on a Windows server and had to tweak some settings to get it to work right. On my Linux servers the standard PHP and WordPress installs work without doing anything to it.

Leave a Reply to Mike Walsh Cancel reply