因此,我正在寻找如何获取调用方的方法名称。我从未见过,也不知道它是否存在。
例:
<?php
class father {
public function db_select(){
echo method that call me;
}
}
class plugin extends father {
public function select_plugin(){
$query = $this->db_select();
}
}
?>
Run Code Online (Sandbox Code Playgroud)
我要写的是在db_select()内部调用方法的方法名称db_select()。
我正在玩一个 Laravel 项目,看看是否可以使用闭包来实现排序接口,我注意到当我dd()闭包时,它还显示了将闭包创建为属性的类。
// in my Order model class, i have a function that will return a closure\npublic static function defaultSortFunction(){\n $sortColumn = property_exists(self::class,'defaultSortingColumn') ? self::$defaultSortingColumn : 'created_at';\n\n return function($p,$n)use($sortColumn){\n return $p->$sortColumn <=> $n->$sortColumn;\n };\n}\nRun Code Online (Sandbox Code Playgroud)\n// in one of my controller I use for testing, I added these 2 methods for testing\npublic function index(){\n $sortFunction = Order::defaultSortFunction();\n $this->someOtherFunction($sortFunction);\n return 'done';\n}\n\nprivate function someOtherFunction($fn){\n dd($fn);\n\n // $scopeModel = get_class($fn); => Closure\n \n // example …Run Code Online (Sandbox Code Playgroud) 以某种方式可能吗?如果是这样的话怎么样?!我知道我可以通过一个参数,但我希望它是动态的!
<?php
class my_class {
protected $parent = NULL;
public function __construct() {
// now i'd like to get the name of the function where this class has been called
$this->parent = get_parent_function();
}
public function parent() {
return $this->parent;
}
}
function some_random_function() {
// Do something
$object = new my_class();
print $object->parent(); // returns: some_random_function
}
?>
Run Code Online (Sandbox Code Playgroud)
提前致谢!