1

Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'info', to be a valid callback in [...]

public function getDisplay(){
    $info = array_merge($this->info,array());

    return  preg_replace_callback('!\{\{(\w+)\}\}!', 'info', $this->display);
}

In a public function from "MyClass", stopped working when I moved from another class to this one. Which was:

public function getEdit( $type){
    $all_info = $this->info;

    return preg_replace_callback('!\{\{(\w+)\}\}!', 'all_info', $edit_contents);
}

Both are cleaned up, and now I can't retest with previous class because it's already long gone.

I'm sure using variables is not allowed, but it was working, so I'm clueless.

When I do this, as suggested in some stackoverflow thread, but obviously it's not made to be used within objects:

public function getDisplay(){
    $display_info = $this->info;

    function display_info($matches) {
        global $display_info;
        return $display_info[$matches[1]];
    }

    return  preg_replace_callback('!\{\{(\w+)\}\}!', 'display_info', $this->display);
}

So I need some love and guidance cuz php is driving me crazy this week...

Vikash Chauhan
  • 792
  • 2
  • 9
  • 18
localhost
  • 450
  • 2
  • 6
  • 24

3 Answers3

14

Instead of using anonymous functions or more complex methods, you can just use one of methods supported for passing callbacks (see the documentation on callbacks):

A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.

To pass "info" method of current object as a callback, just do the following:

array($this, 'info')

and pass it wherever you want to use "info" method as a callback within one of the objects other methods.

Tadeck
  • 132,510
  • 28
  • 152
  • 198
  • 1
    +1 IMO this is the correct way to do it, in other functions which have callbacks as well. Closures seem a bit...cramped, frantic. – Ben Aug 23 '13 at 05:28
4

The correct way to do this would be with a closure:

public function getDisplay() {

    // While $this support in closures has been recently added (>=5.4.0), it's
    // not yet widely available, so we'll get another reference to the current
    // instance into $that first:
    $that = $this;

    // Now we'll write that preg... line of which you speak, with a closure:
    return  preg_replace_callback('!\{\{(\w+)\}\}!', function($matches) use($that) {
        return $that->info[$matches[1]];
    }, $this->display);

}
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • That is soo bootyfull!! Way too nice to not be used! But my version is 5.3.x Damn that Wamp! – localhost Jun 22 '12 at 22:23
  • 5.3 is fine for the above code, it's just if you are 5.2 or less that closures don't work. – DaveRandom Jun 22 '12 at 22:35
  • The 5.4 constraint is just for using the magic variable `$this` within a closure, which is why I created `$that`. The one issue with this is that the property `info` will need to be `public`. – DaveRandom Jun 22 '12 at 22:46
  • This would be nice: `function($matches) use($that) { return $that->getInfo()[$matches[1]]`. But if I remember correctly PHP disallows that... – localhost Jun 22 '12 at 23:58
  • @localhost That syntax is known as array dereferencing and it [works as of 5.4](http://www.php.net/ChangeLog-5.php#5.4.0). No dice if you're stuck with 5.3 though. – DaveRandom Jun 23 '12 at 00:10
  • Although you could make your `getInfo()` method accept an argument, and have it return that specific key if the argument is passed and the whole of `info` if it isn't. Then you can just pass `$matches[1]` into `getInfo()` – DaveRandom Jun 23 '12 at 00:14
2

This solved it:

public function getDisplay(){
    return  preg_replace_callback('!\{\{(\w+)\}\}!', array(get_class($this), '_preg_replace_callback'), $this->display);
}

private function _preg_replace_callback($matches){
    return $this->info[$matches[1]];
}

I did try this approach before, but didn't use the get_class() function to wrap $this. Oh bother...

localhost
  • 450
  • 2
  • 6
  • 24
  • 2
    you shouldn't need get_class(). `array($this, '_preg_replace_callback')` is ok. actually, i don't think your posted solution can work. – goat Jun 22 '12 at 22:17
  • well it did work, and wasn't working without get_class()...It's PHP, go figure... – localhost Jun 22 '12 at 22:21