Php 我如何找到一个闭包绑定到的对象的类名?
我有一些第三方代码,它创建了一个闭包,然后绑定到一个对象。 闭包对象上的打印会产生以下结果:Php 我如何找到一个闭包绑定到的对象的类名?,php,oop,closures,classname,Php,Oop,Closures,Classname,我有一些第三方代码,它创建了一个闭包,然后绑定到一个对象。 闭包对象上的打印会产生以下结果: Closure Object ( [this] => am4Widgets Object ( ) ) 现在我需要检索绑定对象的“instanceof”(在本例中为“am4Widgets”),类似于 print_r(myClosureObject instanceofboundobject am4Widgets); 哪个应该输出“TRUE” 我搜索了php.net,但没有结果 提前感谢您的任何想
Closure Object ( [this] => am4Widgets Object ( ) )
现在我需要检索绑定对象的“instanceof”(在本例中为“am4Widgets”),类似于
print_r(myClosureObject instanceofboundobject am4Widgets);
哪个应该输出“TRUE”
我搜索了php.net,但没有结果
提前感谢您的任何想法/建议
更新:
这里是创建闭包的地方(我无法修改的代码片段):
函数initActions()
{
父::initActions();
.
.
.
添加操作('wp_头',函数(){
$ajax_url=admin_url('admin ajax.php');
echo您可以使用object来实现此目的。
输出:
$this from closure: A
$this from reflection: A
如何定义«Closure object»?请检查:@German Lashevich在本例中,“Closure”是由wordpress的底层框架在传递给add_action()的“functio(){}”块上构建的函数作为第二个参数。@yivi感谢yivi的提示。已经在谷歌上找到了。它非常接近我要寻找的东西,但并不完全相同。第一个答案中的方法将删除相同优先级内的所有闭包,而我正在寻找一种只删除绑定对象标识的闭包的方法。肮脏的黑客会e来封装ob_start()和ob_end_clean()之间的print_r()调用,并搜索字符串(在我的例子中是:am4Widget),但是,正如前面所说的,这是一种黑客行为,一种肮脏的黑客行为。如果可能的话,我宁愿采用更为面向对象的代码方法。
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());
$this from closure: A
$this from reflection: A