Php preg_replace_callback()-当前对象实例内的回调

Php preg_replace_callback()-当前对象实例内的回调,php,Php,警告:preg_replace_callback()[function.preg replace callback]:要求参数2“info”是[…]中的有效回调 public function getDisplay(){ $info = array_merge($this->info,array()); return preg_replace_callback('!\{\{(\w+)\}\}!', 'info', $this->display); } 在“MyC

警告:preg_replace_callback()[function.preg replace callback]:要求参数2“info”是[…]中的有效回调

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

    return  preg_replace_callback('!\{\{(\w+)\}\}!', 'info', $this->display);
}
在“MyClass”的一个公共函数中,当我从另一个类移动到这个类时,停止工作。这是:

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

    return preg_replace_callback('!\{\{(\w+)\}\}!', 'all_info', $edit_contents);
}
这两门课都被清理干净了,现在我不能用上节课重新测试了,因为它已经很久没用了

我确信使用变量是不被允许的,但它是有效的,所以我不知道

当我这样做时,正如某些stackoverflow线程中所建议的,但显然它不是用来在对象中使用的:

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);
}
所以我需要一些爱和指导,因为本周php让我疯狂…

这解决了问题:

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]];
}
我以前确实尝试过这种方法,但没有使用
get_class()
函数包装
$this
。哦,麻烦…

这解决了它:

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]];
}
我以前确实尝试过这种方法,但没有使用
get_class()
函数包装
$this
。哦,麻烦…

正确的方法是关闭:

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);

}
正确的方法是关闭:

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);

}

您不必使用匿名函数或更复杂的方法,只需使用传递回调()所支持的方法之一:

实例化对象的方法作为数组传递,数组包含索引0处的对象和索引1处的方法名称

要将当前对象的“info”方法作为回调传递,只需执行以下操作:

array($this, 'info')

并将其传递到任何您希望在对象或其他方法中使用“info”方法作为回调的位置。

您可以使用支持传递回调()的方法之一,而不是使用匿名函数或更复杂的方法:

实例化对象的方法作为数组传递,数组包含索引0处的对象和索引1处的方法名称

要将当前对象的“info”方法作为回调传递,只需执行以下操作:

array($this, 'info')

并将它传递到任何您想在对象或其他方法中使用“info”方法作为回调的地方。

您不需要get_class()<代码>数组($this,“\u preg\u replace\u callback”)正常。事实上,我认为您发布的解决方案无法工作。它确实工作了,没有get_class(),它就无法工作……这是PHP,想想看……您不应该需要get_class()<代码>数组($this,“\u preg\u replace\u callback”)正常。事实上,我不认为你发布的解决方案可以工作。它确实工作了,没有get_class()…它是PHP,算了吧…这太棒了!!太好了,不能不用!但我的版本是5.3.x该死的笨蛋!5.3对于上面的代码很好,只是如果你是5.2或更低,闭包就不起作用。5.4约束只用于在闭包中使用神奇变量
$this
,这就是我创建
$this
的原因。这样做的一个问题是属性
info
必须是
public
。这很好:
函数($matches)使用($that){return$that->getInfo()[$matches[1]]
。但如果我没记错的话,PHP不允许…@localhost这种语法被称为数组解引用,而且它也不允许。但是如果你坚持使用5.3,那就没用了。这太棒了!!太好了,不能不使用!但是我的版本是5.3.x该死的Wamp!5.3对于上面的代码来说很好,只是如果你是5.2或更低,闭包就不起作用了。5.4约束仅用于在闭包中使用神奇变量
$this
,这就是我创建
$that
的原因。其中的一个问题是属性
info
必须是
public
。这很好:
函数($matches)使用($that){return$that->getInfo()[$matches[1]]
。但是如果我没记错的话,PHP不允许…@localhost这种语法被称为数组解引用,而且它也不允许。但是如果你坚持使用5.3,那就没用了。+1在其他也有回调的函数中,这是正确的方法。闭包似乎有点…局促,疯狂。+1在其他函数中,这是正确的方法也有回调的ns。闭包似乎有点…局促,疯狂。