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 關注
- 195 瀏覽
添加回答
舉報