2 回答

TA贡献1865条经验 获得超7个赞
您可以在闭包上使用 Reflection API,这是一种比debug_backtrace
// in one of my controller I use for testing, I added these 2 methods for testing
public function index(){
$sortFunction = Order::defaultSortFunction();
$this->someOtherFunction($sortFunction);
return 'done';
}
private function someOtherFunction($fn){
$reflectionClosure = new \ReflectionFunction($fn);
dd($reflectionClosure->getClosureScopeClass()->getName());
}
getClosureScopeClassReflectionClass根据您需要查找的类返回一个实例并getName完成作业。

TA贡献1805条经验 获得超9个赞
您当然可以通过 defaultSortFunction 中的参数将类名注入到闭包中,但这显然不太好。
如果您使用 limit 参数,您应该能够将其限制为仅返回调用类,而不再返回。
我不确定,但我怀疑它的性能不是特别好。
- 2 回答
- 0 关注
- 212 浏览
添加回答
举报