Convert anonymous function in PHP 5.3 into PHP 5.2 equivalent

Go To StackoverFlow.com

1

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

2012-04-05 15:09
by Bonn
and the error is... - Bot 2012-04-05 15:11
ups, sorry missed that, here is the error: Parse error: syntax error, unexpected T_FUNCTIO - Bonn 2012-04-05 15:12


3

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;
}
2012-04-05 15:15
by Rocket Hazmat
moving it outside is okay assuming it isn't a closur - newacct 2012-04-06 01:09
@newacct: Pretty sure PHP 5.2 can't do closures anyway - Rocket Hazmat 2012-04-06 01:20


4

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;
}
2012-04-05 15:14
by DaveRandom


0

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.

2012-04-05 15:26
by Luca Rainone
Use Nowdocs (<<<'myfunc') to avoid having to escape the $s ;- - DaveRandom 2012-04-05 15:28
@DaveRandom wow :) that's cool :D I did not know i - Luca Rainone 2012-04-05 15:31
@DaveRandom: You can't use "nowdoc" syntax in PHP 5.2. http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdo - Rocket Hazmat 2012-04-05 15:43
@Rocket You know, I just realised that and came back here to say so but you beat me to it - DaveRandom 2012-04-05 15:51
I'm a chicken. I've re edited it : - Luca Rainone 2012-04-05 15:55
Ads