Page 1 of 1

preg_replace_callback

Posted: Sat Jan 03, 2009 3:14 pm
by kaisellgren
Hi,

I didn't know how to put this in the title. I'm doing a preg_replace_callback to a function named callback_fix() within a class. Here's an example:

Code: Select all

class xxx
 {
  function do()
   {
    function callback_fix($matches)
     {
      return $this -> loader[$matches[1]];
     }
    preg_replace_callback(...,'callback_fix',...);
   }
 }
The problem is that I am using "$this ->" inside the callback function and therefore it will not work. I need to somehow refer to the $this. I know a few ways to do it, but I'm just wondering what's the best way to do it? (Fastest)

Re: preg_replace_callback

Posted: Sat Jan 03, 2009 3:57 pm
by Syntac
Something like this:

Code: Select all

preg_replace_callback(..., array($this, "method_name_here"), ...);

Re: preg_replace_callback

Posted: Sat Jan 03, 2009 4:50 pm
by kaisellgren
Syntac wrote:Something like this:

Code: Select all

preg_replace_callback(..., array($this, "method_name_here"), ...);
Thanks :)