Convert anonymous function in PHP 5.3 into PHP 5.2

2019-02-28 13:19发布

问题:

I have error in line 2 and 13 in PHP 5.2, I have no idea to make the correction, I tried using create_function but not working, can anyone help with this?

function _process_special_keyword($str){
   $callback = function($match){
     $ret = $match[1] . '[' . $match[2] . ']';
     if(!empty($match[3])){
       $ret .= '.[' . $match[3] . ']';
     } 
     $ret .= $match[4];
     return $ret;           
   };

   $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', $callback, $str);

   $callback = function($match){
     return 'CASE WHEN ' . $match[1] . ' THEN ' . $match[2] . ' ELSE ' . $match[3] . ' END';
   };

   $strSQL = preg_replace_callback('/if\s*\((.+),(.+),(.+)\)/i', $callback, $strSQL);
   return $strSQL;
}

Thanks.

Error: Parse error: syntax error, unexpected T_FUNCTION

回答1:

You can declare the callbacks outside of this function. Like this:

function _callback_one($match){
  $ret = $match[1] . '[' . $match[2] . ']';
  if(!empty($match[3])){
    $ret .= '.[' . $match[3] . ']';
  } 
  $ret .= $match[4];
  return $ret;           
}

function _callback_two($match){
  return 'CASE WHEN ' . $match[1] . ' THEN ' . $match[2] . ' ELSE ' . $match[3] . ' END';
}

function _process_special_keyword($str){
   $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', '_callback_one', $str);

   $strSQL = preg_replace_callback('/if\s*\((.+),(.+),(.+)\)/i', '_callback_two', $strSQL);
   return $strSQL;
}

Note: If these functions are in a class (meaning the function would be need to called like $this->_callback_one), pass an array as the "callback" parameter.

function _process_special_keyword($str){
   $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', array($this, '_callback_one'), $str);

   $strSQL = preg_replace_callback('/if\s*\((.+),(.+),(.+)\)/i', array($this, '_callback_two'), $strSQL);
   return $strSQL;
}


回答2:

When using create_function(), the contents of the first argument should be a string representation of the PHP code that would fill the parentheses for the function declaration. The second argument should contain only the code inside the curly braces {} of the function declaration, the actual declaration itself should be omitted.

Try this code:

function _process_special_keyword($str){

   $callback = create_function(
     '$match',
     '
       $ret = $match[1] . "[" . $match[2] . "]";
       if(!empty($match[3])){
         $ret .= ".[" . $match[3] . "]";
       } 
       $ret .= $match[4];
       return $ret;
     '
   );

   $strSQL = preg_replace_callback('/([\s\(\.,])(' . SPECIAL_KEYWORDS . ')(?:\.(' . SPECIAL_KEYWORDS . '))?([\s\)\.,])/i', $callback, $str);

   $callback = create_function(
     '$match',
     'return "CASE WHEN " . $match[1] . " THEN " . $match[2] . " ELSE " . $match[3] . " END";'
   );

   $strSQL = preg_replace_callback('/if\s*\((.+),(.+),(.+)\)/i', $callback, $strSQL);
   return $strSQL;
}


回答3:

according with object question, the faster way I think is something like so,

$f = <<<myfunc
 \$ret = \$match[1] . '[' . \$match[2] . ']';
 if(!empty(\$match[3])){
   \$ret .= '.[' . \$match[3] . ']';
 } 
 \$ret .= \$match[4];
 return \$ret;           
myfunc;

$callback = create_function('$match',$f);

note backslashes before $ and <<< FLAG FLAG; construct. In practice the answer of Rocket is more simple.