2012-04-05 54 views
1

我在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

回答

3

你可以聲明此功能之外的回調。就像這樣:

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

4

當使用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; 
} 
0
與對象的問題,根據

,更快的方式,我認爲是這樣的話,

$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('<<<'myfunc'')避免不得不逃脫'$'s ;-) – 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