[pmwiki-users] Recipes you use/need to be updated for PHP 5.5

John Rankin john.rankin at affinity.co.nz
Sat Jan 4 22:51:49 CST 2014

> John Rankin writes:
>> >> >   http://www.pmwiki.org/wiki/PmWiki/CustomMarkup#php55
>> In a lot of cases, I'm going to need the 'use' keyword to pass
>> parameters to the callback function.
> This will not work for PHP versions 5.2 and older IIRC, and you cannot
> declare inline (native) lambda functions (closures) before PHP 5.3. That's
> why in MarkupToHTML() I export a global array with the $pagename local
> variable and extract() it in the callback functions. If there is a better
> way, that is compatible with new and old PHP versions, I'll gladly review
> it and use it.

Professor Google suggested this alternative, using object-method callback:

class Callback
    public $parameter;

    public function handle($matches)
        return $matches[1] . ($matches[2] + $this->parameter);

$instance = new Callback();
$instance->parameter = 99;
echo preg_replace_callback("|(\d{2}/\d{2}/)(\d{4})|", array($instance,
'handle'), $text);

Any advice on whether I should use global variables or the object-method
to pass variables to the callback function? The code I'm looking at uses
preg_replace with /e a lot, and I'm a little reluctant to use global
variables to pass parameters. TIA

John Rankin

More information about the pmwiki-users mailing list