PHP:如何使用类函数作为回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28954168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: How to use a class function as a callback
提问by SmxCde
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)
UPD:如何从static
方法中执行相同的操作(当$this
不可用时)
回答by MikO
Check the callable
manualto 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
手册以查看将函数作为回调传递的所有不同方法。我在此处复制了该手册,并根据您的场景添加了每种方法的一些示例。
Callable
可调用
- A PHP functionis 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(), printor unset().
- 一个PHP函数是由它的名字作为字符串传递。可以使用任何内置或用户定义的函数,但语言结构除外,例如:array()、echo、empty()、eval()、exit()、isset()、list()、print或unset().
// Not applicable in your scenario
$this->processSomething('some_global_php_function');
- A method of an instantiated objectis passed as an array containing an object at index 0and the method name at index 1.
- 甲一个实例化的对象的方法为含有索引对象的数组传递0和索引方法名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 methodscan also be passed without instantiating an object of that class by passing the class name instead of an object at index 0.
- 也可以通过传递类名而不是索引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 functionscan also be passed to a callback parameter.
- 除了常见的用户自定义函数,匿名函数也可以传递给回调参数。
// Not applicable in your scenario unless you modify the structure
$this->processSomething(function() {
// process something directly here...
});
回答by Bankzilla
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
由于 5.3 有一种更优雅的方式可以编写它,我仍在尝试找出它是否可以减少更多
$this->processSomething(function() {
$this->myCallback();
});
回答by Geovani Santos
You can also to use call_user_func() to specify a callback:
您还可以使用 call_user_func() 来指定回调:
public function myMethod() {
call_user_func(array($this, 'myCallback'));
}
private function myCallback() {
// do something...
}