Struggling to understand closures for a couple of days. Can anybody point me in the right direction? Need to re-write this "create_function" as a lambda.
$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);
You define a closure like so:
$myClosure = function ($args) {
// do something;
};
create_function
takes two arguments - the first is an eval'd string of the callable's arguments, the second is the code to execute - so you'd do something like this:
$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
);
Note that I've replaced your global variable calls with importing them via use
into the callback instead, and removed $lang
because you aren't using it.