2017-09-25 18 views
0

数日間の閉鎖を理解するために苦労しています。誰かが私を正しい方向に向けることができますか?この "create_function"をラムダとして書き直す必要があります。create_functionからclosuresへのアップグレード

$section = preg_replace_callback('/{listing_field_([^{}]*?)_caption}/', create_function('$matches', 'global $config,$or_replace_listing_id,$lang;require_once($config[\'basepath\'].\'/include/listing.inc.php\'); return listing_pages::renderSingleListingItem($or_replace_listing_id, $matches[1],\'caption\');'), $section); 

答えて

1

あなたがそうのような閉鎖を定義します。

$myClosure = function ($args) { 
    // do something; 
}; 

create_functionは2つの引数を取ります - 2番目を実行するためのコードです、最初は呼び出し可能なの引数のeval'd文字列である - ので、あなたの」私が代わりにコールバックへuse経由でインポートすると、あなたのグローバル変数の呼び出しを置き換え、およびを削除しました

$section = preg_replace_callback(
    // Your regex search pattern 
    '/{listing_field_([^{}]*?)_caption}/', 
    // Your callback 
    function ($matches) use ($config, $or_replace_listing_id) { 
     require_once $config['basepath'] . '/include/listing.inc.php'; 
     return listing_pages::renderSingleListingItem(
      $or_replace_listing_id, 
      $matches[1], 
      'caption' 
     ); 
    }, 
    // Your subject 
    $section 
); 

注:このような何かを行うdはを使用していないためです。

関連する問題