在 PHP 中将实例方法作为参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13543143/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:39:31  来源:igfitidea点击:

Passing an instance method as argument in PHP

phpclassmethodsargument-passing

提问by Gergely Fehérvári

I would like to create a Listener class

我想创建一个监听器类

class Listener {
    var $listeners = array();

    public function add(callable $function) {
        $this->listeners[] = $function;
    }

    public function fire() {
        foreach($this->listeners as $function) {
            call_user_func($function);
        }
    }
}

class Foo {
    public function __construct($listener) {
        $listener->add($this->bar);
    }

    public function bar() {
        echo 'bar';
    }
}



$listener = new Listener();
$foo = new Foo($listener);

But this code fails with this error:

但是此代码失败并出现此错误:

Notice: Undefined property: Foo::$bar in index.php on line 18

Catchable fatal error: Argument 1 passed to Listener::add() must be callable, null given, called in index.php on line 18 and defined index.php on line 5

注意:第 18 行 index.php 中的未定义属性:Foo::$bar

可捕获的致命错误:传递给 Listener::add() 的参数 1 必须是可调用的,给定 null,在第 18 行的 index.php 中调用并在第 5 行定义了 index.php

What am I doing wrong?

我究竟做错了什么?

采纳答案by rid

  • Before PHP 5.4, there was no type named callable, so if you use it as a type hint, it means "the class named callable". If you use PHP >= 5.4, callableis a valid hint.

  • A callable is specified by a string describing the name of the callable (a function name or a class method name for example) or an array where the first element is an instance of an object and the second element is the name of the method to be called.

  • 在 PHP 5.4 之前,没有名为 named 的类型callable,因此如果将其用作类型提示,则表示“名为名为的类callable”。如果您使用 PHP >= 5.4,这callable是一个有效的提示。

  • 可调用对象由描述可调用对象名称的字符串(例如函数名称或类方法名称)或数组指定,其中第一个元素是对象的实例,第二个元素是要调用的方法的名称叫。

For PHP < 5.4, replace

对于 PHP < 5.4,替换

public function add(callable $function)

with:

和:

public function add($function)

Call it with:

调用它:

$listener->add(array($this, 'bar'));

回答by phant0m

Methods and properties have separate namespaces in PHP, which is why $this->barevaluates to null: You're accessing an undefined property.

方法和属性在 PHP 中具有单独的命名空间,这就是为什么$this->bar计算结果为null:您正在访问未定义的属性

The correct way to create an array in the form of array($object, "methodName"):

以以下形式创建数组的正确方法array($object, "methodName")

Passing the callbackcorrectly:

正确传递回调

$listener->add(array($this, 'bar'));  


The type hintyou have given is okay—as of PHP 5.4, that is.

您给出的类型提示没问题——从 PHP 5.4 开始,就是这样。

回答by luiges90

I don't think you can specify a callable this way...

我认为您不能以这种方式指定可调用对象...

Try

尝试

$listener->add(array($this, 'bar'));

And see http://php.net/manual/en/language.types.callable.phptoo.

并参见http://php.net/manual/en/language.types.callable.php