PHP将附加参数传递给回调函数

时间:2022-12-08 19:34:04

Is there a way to pass additional parameters to a callback function?

是否有方法将附加参数传递给回调函数?

class Bar{

  public function test(){
      $count = 0; 

      $foo = new Class();
      $foo->calling([$this, 'barFunction']);
  }

  public function barFunction($data, $count){
      //...
  }

I want Foo's calling method to pass data in and to also pass $count as the second parameter.

我希望Foo的调用方法将数据传入,并将$count作为第二个参数传递。

Here is an example with a closure instead of a parameter. If I use a closure I can pass $count in but in this case I need to use Bar's class function.

下面是一个使用闭包而不是参数的示例。如果我使用闭包,我可以传入$count,但是在这种情况下,我需要使用Bar的类函数。

class Bar{

  public function test(){
      $count = 0; 

      $foo = new Class();
      $foo->calling(function($data) use($count){
         //..some work
      });
  }

3 个解决方案

#1


3  

Could use call_user_func_array

可以使用中的call_user_func_array

call_user_func_array([$context, $function], $params);

中的call_user_func_array([背景下,函数美元],美元params);

i.e. in your example, $context would be Bar ($this), $function would be your 'barFunction', and the params would be [$data, $count].

例如,在您的示例中,$context将是Bar ($this), $function将是您的“barFunction”,params将是[$data, $count]。

Like this?

像这样的吗?

<?php 

class Foo {
    public function __construct() {}

    public function mDo($callback) {
        $count = 10;
        $data = ['a', 'b'];

        // do a lot of work (BLOCKING)

        call_user_func_array($callback, [$data, $count]);
    }
}

class Bar {
    public function test() {
        $a = new Foo();
        $a->mDo([$this, 'callback']);
    }

    public function callback($data, $count) {
        print($data);
        print($count);
    }
}

$bar = new Bar();
$bar->test();

#2


2  

OOP is your friend. Declare the callback as a class:

面向对象是你的朋友。将回调声明为类:

class BarCallback {
    private $count;

    public function __construct($count) {
        $this->count = $count;
    }

    public function callback($data) {
        // do work here
    }
}

And then Bar would look something like this:

然后Bar看起来是这样的

class Bar {
  public function test() {
      $count = 0; 

      $foo = new Class();
      $bar_cb = new BarCallback($count);
      $foo->calling([$bar_cb, 'callback']);
  }
}

Does that help?

这有帮助吗?

#3


-1  

Here is my workaround I'm wrapping the class method call in a closure and passing $that because of PHP's lack of support for it. Even though the object is passed by reference to $that it works.

下面是我的解决方案,我将类方法调用封装在一个闭包中,并传递$that,因为PHP不支持它。即使该对象是通过引用$来传递的,它仍然有效。

See: Why can I not use $this as a lexical variable in PHP 5.5.4?

参见:为什么我不能在PHP 5.5.4中将$this用作词法变量?

class Bar{

public function test(){
  $that = $this; // Workaround for PHP's  "Cannot use $this as lexical"
  $count = 0; 

  $foo = new Class();
  $foo->calling(function($data) use($that,$count){
     $that->barFunction($data, $count);
  });

  public function barFunction($data, $count){
    //...
  }

}

}

#1


3  

Could use call_user_func_array

可以使用中的call_user_func_array

call_user_func_array([$context, $function], $params);

中的call_user_func_array([背景下,函数美元],美元params);

i.e. in your example, $context would be Bar ($this), $function would be your 'barFunction', and the params would be [$data, $count].

例如,在您的示例中,$context将是Bar ($this), $function将是您的“barFunction”,params将是[$data, $count]。

Like this?

像这样的吗?

<?php 

class Foo {
    public function __construct() {}

    public function mDo($callback) {
        $count = 10;
        $data = ['a', 'b'];

        // do a lot of work (BLOCKING)

        call_user_func_array($callback, [$data, $count]);
    }
}

class Bar {
    public function test() {
        $a = new Foo();
        $a->mDo([$this, 'callback']);
    }

    public function callback($data, $count) {
        print($data);
        print($count);
    }
}

$bar = new Bar();
$bar->test();

#2


2  

OOP is your friend. Declare the callback as a class:

面向对象是你的朋友。将回调声明为类:

class BarCallback {
    private $count;

    public function __construct($count) {
        $this->count = $count;
    }

    public function callback($data) {
        // do work here
    }
}

And then Bar would look something like this:

然后Bar看起来是这样的

class Bar {
  public function test() {
      $count = 0; 

      $foo = new Class();
      $bar_cb = new BarCallback($count);
      $foo->calling([$bar_cb, 'callback']);
  }
}

Does that help?

这有帮助吗?

#3


-1  

Here is my workaround I'm wrapping the class method call in a closure and passing $that because of PHP's lack of support for it. Even though the object is passed by reference to $that it works.

下面是我的解决方案,我将类方法调用封装在一个闭包中,并传递$that,因为PHP不支持它。即使该对象是通过引用$来传递的,它仍然有效。

See: Why can I not use $this as a lexical variable in PHP 5.5.4?

参见:为什么我不能在PHP 5.5.4中将$this用作词法变量?

class Bar{

public function test(){
  $that = $this; // Workaround for PHP's  "Cannot use $this as lexical"
  $count = 0; 

  $foo = new Class();
  $foo->calling(function($data) use($that,$count){
     $that->barFunction($data, $count);
  });

  public function barFunction($data, $count){
    //...
  }

}

}