如何找出闭包绑定到的对象的类名?

3 php oop closures classname

我有一些第三方代码,它创建一个闭包,然后将其绑定到一个对象。闭包对象上的 print_r 会产生以下结果:

Closure Object ( [this] => am4Widgets Object ( ) )
Run Code Online (Sandbox Code Playgroud)

现在我需要检索绑定对象的“instanceof”(在本例中为“am4Widgets”),某种伪代码,例如

print_r(myClosureObject instanceofboundobject am4Widgets);
Run Code Online (Sandbox Code Playgroud)

应该输出“TRUE”。

我搜索了 php.net 但没有结果。

预先感谢您的任何想法/建议。

更新:

这是创建闭包的位置(我无法修改的代码片段):

function initActions()
{
    parent::initActions();
    .
    .
    .
    add_action('wp_head', function(){
        $ajax_url =  admin_url( 'admin-ajax.php' );
        echo <<<CUT
<script>...some javascript code...</script>
CUT;
    });
}
Run Code Online (Sandbox Code Playgroud)

实际上,我想做的是从 wp_head 中取消闭包,因为我需要它在页脚中。

我正在使用全局 wordpress 的 $wp_filters 来访问所有已注册的钩子,但现在我需要一种方法来唯一标识我想要取消钩子的闭包,如果有一种方法可以访问闭包的绑定对象,这可能是一项简单的任务。

Ger*_*ich 5

您可以使用ReflectionFunction对象来达到此目的。

class A {}

$closure = (function () {
    echo '$this class from closure: ' . get_class($this) . "\n";
})->bindTo(new A());

$closure();

$fn = new ReflectionFunction($closure);
echo '$this class from reflection: ' . get_class($fn->getClosureThis());
Run Code Online (Sandbox Code Playgroud)

输出:

$this from closure: A
$this from reflection: A
Run Code Online (Sandbox Code Playgroud)