Search code examples
phpphpmailer

Why did PHPMailer just change from using dynamic function calls to call_user_func()?


While vetting the latest update to PHPMailer (PHP library for sending e-mails), I noticed this change:

return $patternselect($address);

Into:

return call_user_func($patternselect, $address);

At first, I had to make sure that I wasn't looking at this in a "reverse" view, which I wasn't; the call_user_func change is the new code -- not the old. I also noticed the same being done in a different place of the code.

So, they decided to stop using the nice "new" syntax for dynamically calling a function and instead changed it into the old call_user_func syntax.

Why? Am I missing something? Wasn't the first code shown here (which is what I always use) an improvement to PHP which made it nicer to call functions dynamically without having to use call_user_func in your code? Why would they "go back" to the old way?

It's not like this was just added to PHP or anything, so it can't be a case of a buggy, bleeding-edge feature which "isn't quite ready yet".

PS: Note that I'm not asking you to look into the brains of the PHPMailer developer(s), but to offer me an explanation as to why anyone would want to actively do this once they had actually used the better (or so I thought?) way.


Solution

  • Since this is so specific, it would probably have been better asked on Github, but I'll answer it here.

    This was done for a very specific reason; switching to variable functions (which was done in PHPMailer 6.1.2) caused a very specific BC break in PHP versions between 5.2.3 and 7.0.0, and that was reported to me. While those PHP versions are now considered defunct, many are still using them, and the current PHPMailer release (6.x) promises compatibility back to PHP 5.5, so this change needed be reverted to retain compatibility with those old versions. It has no effect on later PHP versions.

    I originally changed to variable functions because, as you say, it's the new shiny, it's easier to read (IMHO), and it's the kind of thing that gets recommended by static analysers, however these (evidently!) don't take backward compatibility into account.

    So what was the BC break? While variable functions themselves have been around for a while (since at least PHP 5.4 I think), the static method callable pattern (class::method) was only added in PHP 7.0.0, whereas that syntax works fine in call_user_func; that's exactly what the BC break was, and the reason for the reversion in the PHPMailer 6.1.7 release.