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

Petko Yotov 5ko at 5ko.fr
Sun Jan 5 04:46:57 CST 2014


John Rankin writes:
> > 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.
>
> 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);

This is very interesting and possibly usable for the core. It is cleaner to  
pass variables this way, even if it may be less concise.

> 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.

I am reluctant too to use global variables, but that's a single global  
variable - array - holding the local variables.

Classes and callbacks have changes in different PHP versions so it may be  
tricky to write one that is compatible with PHP 4.2 to 5.5. But not  
impossible.

Petko




More information about the pmwiki-users mailing list