将PHP 5.3中的匿名函数转换为PHP 5.2等效

问题描述:

我在PHP 5.2中有第2行和第13行的错误,我不知道要进行更正,我尝试使用create_function但不工作,任何人都可以帮助解决这个问题吗?将PHP 5.3中的匿名函数转换为PHP 5.2等效

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

+0

并且错误是...? – Bot 2012-04-05 15:11:20

+0

ups,对不起错过了,这里是错误:解析错误:语法错误,意外T_FUNCTION – Bonn 2012-04-05 15:12:57

你可以声明此功能之外的回调。就像这样:

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),传递一个数组作为“回调”参数。

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

移动它外面是好的,假设它不是封闭 – newacct 2012-04-06 01:09:12

+0

@newacct:很确定PHP 5.2无论如何不能关闭。 – 2012-04-06 01:20:04

当使用create_function(),第一个参数的内容应该是PHP代码,将填补括号中function声明的字符串表示。第二个参数应该只包含函数声明的花括号{}中的代码,实际声明本身应该被忽略。

试试这个代码:

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

与对象的问题,根据

,更快的方式,我认为是这样的话,

$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); 

注$之前反斜杠和< < < FLAG FLAG;构造。在实践中,火箭的答案更简单。

+0

使用Nowdocs(' DaveRandom 2012-04-05 15:28:09

+0

@DaveRandom哇:)这很酷:DI不知道它 – 2012-04-05 15:31:11

+0

@DaveRandom:你不能使用“ nowdoc“语法在PHP 5.2中。 http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc – 2012-04-05 15:43:25