PHP: How to use a class function as a callback [du

2019-01-13 17:10发布

This question already has an answer here:

I have a class with methods which I want to use as callbacks. How to pass them as arguments?

Class MyClass {

    public function myMethod() {
        $this->processSomething(this->myCallback); // How it must be called ?
        $this->processSomething(self::myStaticCallback); // How it must be called ?
    }

    private function processSomething(callable $callback) {
        // process something...
        $callback();
    }

    private function myCallback() {
        // do something...
    }

    private static function myStaticCallback() {
        // do something...
    }   

}

UPD: How to do the same but from static method (when $this is not available)

标签: php oop callback
3条回答
冷血范
2楼-- · 2019-01-13 17:35

Check the callable manual to see all the different ways to pass a function as a callback. I copied that manual here and added some examples of each approach based on your scenario.

Callable


  • A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs such as: array(), echo, empty(), eval(), exit(), isset(), list(), print or unset().
  // Not applicable in your scenario
  $this->processSomething('some_global_php_function');

  • A method of an instantiated object is passed as an array containing an object at index 0 and the method name at index 1.
  // Only from inside the same class
  $this->processSomething([$this, 'myCallback']);
  $this->processSomething([$this, 'myStaticCallback']);
  // From either inside or outside the same class
  $myObject->processSomething([new MyClass(), 'myCallback']);
  $myObject->processSomething([new MyClass(), 'myStaticCallback']);

  • Static class methods can also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.
  // Only from inside the same class
  $this->processSomething([__CLASS__, 'myStaticCallback']);
  // From either inside or outside the same class
  $myObject->processSomething(['\Namespace\MyClass', 'myStaticCallback']);
  $myObject->processSomething(['\Namespace\MyClass::myStaticCallback']); // PHP 5.2.3+
  $myObject->processSomething([MyClass::class, 'myStaticCallback']); // PHP 5.5.0+

  • Apart from common user-defined function, anonymous functions can also be passed to a callback parameter.
  // Not applicable in your scenario unless you modify the structure
  $this->processSomething(function() {
      // process something directly here...
  });

查看更多
Ridiculous、
3楼-- · 2019-01-13 17:35

Since 5.3 there is a more elegant way you can write it, I'm still trying to find out if it can be reduced more

$this->processSomething(function() {
    $this->myCallback();
});
查看更多
淡お忘
4楼-- · 2019-01-13 17:42

You can also to use call_user_func() to specify a callback:

public function myMethod() {

    call_user_func(array($this, 'myCallback'));
}

private function myCallback() {
    // do something...
}
查看更多
登录 后发表回答