Reputation: 2806
I want get the method name. For example, A url call the method1
, and call the private method2
, the method2
want to know who call this method. How could I do it.
I can use __FUNCTION__
or $this->router->method
on the method1
, and put it as method2
parameter. But I want to know could I on the method2
to get this method is called from method1
? Thanks a million.
<?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';
}
}
Upvotes: 1
Views: 1287
Reputation: 1166
You can use the debug_backtrace() function. i guess its the only way to know if you cant use parameters.
$callers=debug_backtrace();
echo $callers[1]['function'];
if you just want to know the previous method which calls, use array_shift. it will show result like-
$caller=array_shift($callers);
echo "Called by {$caller['function']}";
Upvotes: 2
Reputation: 4829
This will depend on your url but u can do something like this..
function getNameOfOriginatingClass{
$this->load->library('user_agent');
$previous_url = $this->agent->referrer();
$url_segments = explode('/',$previous_url);
echo '<pre>';print_r($url_segments);
}
after printing this result u can see your link broken into parts in an array.. Normally the $url_segments[3] or $url_segments[4] will contain your previous function name and previous one will contain previous class name depending upon your url.
Upvotes: 0