As of php 5.5 I am getting the message:
Deprecated function: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in ldap_pear_hex2asc()

Attached is my attempt to fix, but I am not entirely certain of the syntax, namely the double single-quote usage.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jay.dansand’s picture

Version: 7.x-2.x-dev » 7.x-1.x-dev
Issue summary: View changes
FileSize
1.1 KB

I was just coming to create an issue of my own, but hooray there's already one here! Taking a quick look at HEAD of 7.x-2.x, this issue is already fixed:

    function ldap_pear_hex2asc($string) {

      if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
        $string = preg_replace_callback(
          "/\\\([0-9A-Fa-f]{2})/",
          function ($m){
            return chr(hexdec($m[1]));
          },
          $string);
      }
      else {
        $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
      }

      return $string;
    }

So I'm changing this issue to 7.x-1.x-dev (thousands of folks are still on the 7.x-1.x branch, and the project page says "It is recommended to stick with whatever version of it you have working currently until 7.x-2.0RCs come out," so there will probably continue to be many users of the 7.x-1.x branch even on PHP 5.5.

Anonymous functions break PHP support prior to PHP 5.3.0 (see PHP manual entry for Anonymous Functions), and Drupal 7 supports PHP 5.2.5+ (see Drupal's System requirements). That means, to keep an anonymous-ish solution while staying within Drupal's supported systems, we need to fall back on create_function(). I'm attaching a patch against 7.x-1.x-dev that uses create_function() (we're using this in production).

kenorb’s picture

Status: Active » Needs review
johnbarclay’s picture

This still throws an error for me on 7.x-2.0-dev. The following works and is more readable (to me)

    function ldap_pear_hex2asc($string) {


      if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
        $f = create_function('$m', "return chr(hexdec($m[1]));");
        $string = preg_replace_callback("/\\\([0-9A-Fa-f]{2})/",$f,$string);
      }
      else {
        $string = preg_replace("/\\\([0-9A-Fa-f]{2})/e", "''.chr(hexdec('\\1')).''", $string);
      }

      return $string;
    }
jay.dansand’s picture

In the latest LDAP 7.x-2.x-dev, the "/e modifier is deprecated" notice should never happen, because either:

  • PHP version is 5.3.0+, in which case an anonymous function and preg_replace_callback() are used (no /e modifier), or
  • PHP version is less than 5.3.0, in which case the /e modifier is not deprecated yet (/e is deprecated in PHP 5.5.x)

So if you're experiencing the problem, I suggest either using the latest 7.x-2.x-dev (I just double-checked the code and it should still be good - but if you're on the latest version and still getting this notice, and you're sure your Zend Opcache has been cleared and that it isn't running into a cache collision with someone else's older LDAP installation running on the same machine, then let's get to the bottom of it!), or copying that bit of the code out of 7.x-2.x-dev and applying it to whatever version you are using.

At the very least, I'd definitely recommend changing your patch, because every time create_function() is invoked, even if it's to create the same exact function, it runs eval() and creates an additional copy of the lambda_N function in the global space. This starts gobbling up memory and also slows down PHP (see the manual page for create_funciton()), so it's a best practice to cache the output of create_function() in a static variable and then reuse that:

static $preg_callback = NULL;
if (!isset($preg_callback)) {
  $preg_callback = create_function(...);
}
preg_replace_callback(..., $preg_callback);

But really, in PHP 5.3.0+, it's recommended to use proper anonymous functions, as 7.x-2.x-dev does. Quote from php.net's create_function() manual entry:

If you are using PHP 5.3.0 or newer a native anonymous function should be used instead.

jay.dansand’s picture

  • grahl committed 8069d99 on 7.x-1.x authored by jay.dansand
    Issue #2043235 by jay.dansand, thekevinday: PHP 5.5, preg_replace /e is...
grahl’s picture

Status: Needs review » Fixed

I've backported the current 2.x version which is still different. < 5.3 is dropped.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.