我想获得方法名。例如,一个url调用method1
,并调用私有method2
, method2
想知道是谁调用了这个方法。我怎么能这么做呢?
我可以在method1
上使用__FUNCTION__
或$this->router->method
,并将其作为method2
参数。但是我想知道我能否在method2
上得到这个方法是从method1
调用的?万分感谢。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ControllerName extends CI_Controller {
public function __construct()
{
parent::__construct();
}
public function method1()
{
$this->_method2()
}
private function _method2()
{
// How to get call method name method1,
// Don't use parameter
// $who_call_me = 'method1';
}
}
可以使用debug_backtrace()函数。我想这是知道你是否不能使用参数的唯一方法。
$callers=debug_backtrace();
echo $callers[1]['function'];
如果你只想知道前面调用的方法,使用array_shift。它将显示像-
这样的结果$caller=array_shift($callers);
echo "Called by {$caller['function']}";
这取决于你的url,但你可以这样做…
function getNameOfOriginatingClass{
$this->load->library('user_agent');
$previous_url = $this->agent->referrer();
$url_segments = explode('/',$previous_url);
echo '<pre>';print_r($url_segments);
}
打印结果后,你可以看到你的链接在数组中被分解成几个部分。通常$url_segments[3]或$url_segments[4]将包含你之前的函数名,前一个将包含你之前的类名,这取决于你的url。