Is it possible with php to directly call a callback stored in an member variable of a class? currently I'm using a workaround, where I'm temporarily storing my callback to a local var.
class CB {
  private $cb;
  public function __construct($cb) {
    $this->cb = $cb;
  }
  public function call() {
    $this->cb(); // does not work
    $cb = $this->cb;
    $cb(); // does work
  }
}
php complains that $this->cb() is not a valid method, i.e. does not exist.
In php7 you can call it like this:
class CB {
  /** @var callable */
  private $cb;
  public function __construct(callable $cb) {
    $this->cb = $cb;
  }
  public function call() {
    ($this->cb)();
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With