Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在对象上下文中运行的回调函数?_Php_Php 5.3 - Fatal编程技术网

Php 在对象上下文中运行的回调函数?

Php 在对象上下文中运行的回调函数?,php,php-5.3,Php,Php 5.3,我试图在运行时通过回调函数配置对象,如下所示: class myObject{ protected $property; protected $anotherProperty; public function configure($callback){ if(is_callable($callback)){ $callback(); } } } $myObject = new myObject(); // $myObject->configu

我试图在运行时通过回调函数配置对象,如下所示:

class myObject{
  protected $property;
  protected $anotherProperty;

  public function configure($callback){
    if(is_callable($callback)){
      $callback();
    }
  }
}

$myObject = new myObject(); //
$myObject->configure(function(){
  $this->property = 'value';
  $this->anotherProperty = 'anotherValue';
});
当然,我会遇到以下错误:

致命错误:不在对象上下文中使用$this

我的问题是,是否有一种方法可以在回调函数中使用
$this
来实现这种行为,或者可以得到一个更好模式的建议


PS:我更喜欢使用回调。

从您的想法开始,您可以将
$this
作为参数传递给回调

但是请注意,您的回调(未在类中声明)将无法访问受保护的或私有的属性/方法——这意味着您必须设置公共方法来访问这些属性/方法


然后,您的类将如下所示:

class myObject {
  protected $property;
  protected $anotherProperty;
  public function configure($callback){
    if(is_callable($callback)){
      // Pass $this as a parameter to the callback
      $callback($this);
    }
  }
  public function setProperty($a) {
    $this->property = $a;
  }
  public function setAnotherProperty($a) {
    $this->anotherProperty = $a;
  }
}
$myObject = new myObject(); //
$myObject->configure(function($obj) {
  // You cannot access protected/private properties or methods
  // => You have to use setters / getters
  $obj->setProperty('value');
  $obj->setAnotherProperty('anotherValue');
});
您声明了回调,并使用它,如下所示:

class myObject {
  protected $property;
  protected $anotherProperty;
  public function configure($callback){
    if(is_callable($callback)){
      // Pass $this as a parameter to the callback
      $callback($this);
    }
  }
  public function setProperty($a) {
    $this->property = $a;
  }
  public function setAnotherProperty($a) {
    $this->anotherProperty = $a;
  }
}
$myObject = new myObject(); //
$myObject->configure(function($obj) {
  // You cannot access protected/private properties or methods
  // => You have to use setters / getters
  $obj->setProperty('value');
  $obj->setAnotherProperty('anotherValue');
});

紧接着调用以下代码行:

var_dump($myObject);
将输出以下内容:

object(myObject)[1]
  protected 'property' => string 'value' (length=5)
  protected 'anotherProperty' => string 'anotherValue' (length=12)
这表明回调已经执行,并且对象的属性确实已经按照预期设置。

如果您正在使用(或愿意升级到)PHP5.4,您可以使用新的方法。这允许您将闭包“重新绑定”到新范围

在调用
$callback
之前,您可以将其
$this
设置为所需

if(is_callable($callback)){
    $callback = $callback->bindTo($this, $this);
    $callback();
}
演示:

您也可以在类外使用
bindTo

$func = function(){
  $this->property = 'value';
  $this->anotherProperty = 'anotherValue';
};
$myObject->configure($func->bindTo($myObject, $myObject));

演示:

+1这是将PHP升级到其最新版本的另一个原因;-)+1这是一个很好的答案,但不幸的是,我必须保持代码与PHP5.3兼容。*(都添加了5.3标签)@marcioAlmada:我还是把这个留在这里吧:-P+1这是向前迈出的一大步。在接受之前,我将等待一个可能更好的答案。你能描述一下这个代码的工作流程吗?有些事情我无法理解…我想完全理解…$callback($this)中的$this会是$callback($this,'func')吗?谢谢