将PHP 5.3中的匿名函数转换为PHP 5.2等效项 [英] Convert anonymous function in PHP 5.3 into PHP 5.2 equivalent

查看:189
本文介绍了将PHP 5.3中的匿名函数转换为PHP 5.2等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有错误在第2行和13在PHP 5.2,我不知道进行更正,我尝试使用create_function但不工作,任何人可以帮助这个?

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;
}

谢谢。

错误:解析错误:语法错误,意外的T_FUNCTION

Error: Parse error: syntax error, unexpected T_FUNCTION

推荐答案

您可以在此函数之外声明回调。像这样:

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;
}

注意:如果这些函数在一个类中调用像 $ this-> _callback_one ),传递一个数组作为callback参数。

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;
}

这篇关于将PHP 5.3中的匿名函数转换为PHP 5.2等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆