Tuesday, June 21, 2011

When using a PHP function that requires a callback, how do we pass a custom parameter to the 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);

Reference 
  1. http://stackoverflow.com/questions/1502626/when-using-a-php-function-that-requires-a-callback-how-do-we-pass-a-custom-param

No comments:

Post a Comment