Esiste una funzione PHP per scoprire il nome della funzione chiamante in una determinata funzione?
Esiste una funzione PHP per scoprire il nome della funzione chiamante in una determinata funzione?
Risposte:
Vedi debug_backtrace : questo può tracciare il tuo stack di chiamate fino in cima.
Ecco come otterresti il tuo chiamante:
$trace = debug_backtrace();
$caller = $trace[1];
echo "Called by {$caller['function']}";
if (isset($caller['class']))
echo " in {$caller['class']}";
list(, $caller) = debug_backtrace(false);per ottenere il chiamante, falseper le prestazioni ;-) (php5.3)
echo 'called by '.$trace[0]['function']
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];per ottenere il nome del chiamante con prestazioni migliori.
Xdebug offre alcune funzioni interessanti.
<?php
Class MyClass
{
function __construct(){
$this->callee();
}
function callee() {
echo sprintf("callee() called @ %s: %s from %s::%s",
xdebug_call_file(),
xdebug_call_line(),
xdebug_call_class(),
xdebug_call_function()
);
}
}
$rollDebug = new MyClass();
?>
restituirà traccia
callee() called @ /var/www/xd.php: 16 from MyClass::__construct
Per installare Xdebug su Ubuntu il modo migliore è
sudo aptitude install php5-xdebug
Potrebbe essere necessario installare prima php5-dev
sudo aptitude install php5-dev
Questo è molto tardi, ma vorrei condividere la funzione che darà il nome della funzione da cui viene chiamata la funzione corrente.
public function getCallingFunctionName($completeTrace=false)
{
$trace=debug_backtrace();
if($completeTrace)
{
$str = '';
foreach($trace as $caller)
{
$str .= " -- Called by {$caller['function']}";
if (isset($caller['class']))
$str .= " From Class {$caller['class']}";
}
}
else
{
$caller=$trace[2];
$str = "Called by {$caller['function']}";
if (isset($caller['class']))
$str .= " From Class {$caller['class']}";
}
return $str;
}
Spero sia utile.
debug_backtrace() fornisce dettagli di parametri, chiamate funzione / metodo nello stack di chiamate corrente.
echo debug_backtrace()[1]['function'];
Funziona da PHP 5.4 .
O ottimizzato (ad esempio per casi d'uso non di debug):
echo debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
Il primo argomento impedisce di popolare argomenti di funzioni inutilizzate, il secondo limita la traccia a due livelli (è necessario il secondo).
Fatto questo e usando questo me stesso
/**
* Gets the caller of the function where this function is called from
* @param string what to return? (Leave empty to get all, or specify: "class", "function", "line", "class", etc.) - options see: http://php.net/manual/en/function.debug-backtrace.php
*/
function getCaller($what = NULL)
{
$trace = debug_backtrace();
$previousCall = $trace[2]; // 0 is this call, 1 is call in previous function, 2 is caller of that function
if(isset($what))
{
return $previousCall[$what];
}
else
{
return $previousCall;
}
}
Volevo solo affermare che il modo di flori non funzionerà come una funzione perché restituirà sempre il nome della funzione chiamata invece del chiamante, ma non ho la reputazione di commentare. Ho fatto una funzione molto semplice basata sulla risposta di flori che funziona bene per il mio caso:
class basicFunctions{
public function getCallerFunction(){
return debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['function'];
}
}
function a($authorisedFunctionsList = array("b")){
$ref = new basicFunctions;
$caller = $ref->getCallerFunction();
if(in_array($caller,$authorisedFunctionsList)):
echo "Welcome!";
return true;
else:
echo "Unauthorised caller!";
return false;
endif;
}
function b(){
$executionContinues = $this->a();
$executionContinues or exit;
//Do something else..
}
È possibile estrarre queste informazioni dall'array restituito da debug_backtrace
Questo ha funzionato meglio per me: var_dump(debug_backtrace());
In realtà penso che debug_print_backtrace () faccia quello che ti serve. http://php.net/manual/en/function.debug-print-backtrace.php
Questo lo farà bene:
// Outputs an easy to read call trace
// Credit: https://www.php.net/manual/en/function.debug-backtrace.php#112238
// Gist: https://gist.github.com/UVLabs/692e542d3b53e079d36bc53b4ea20a4b
Class MyClass{
public function generateCallTrace()
{
$e = new Exception();
$trace = explode("\n", $e->getTraceAsString());
// reverse array to make steps line up chronologically
$trace = array_reverse($trace);
array_shift($trace); // remove {main}
array_pop($trace); // remove call to this method
$length = count($trace);
$result = array();
for ($i = 0; $i < $length; $i++)
{
$result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
}
return "\t" . implode("\n\t", $result);
}
}
// call function where needed to output call trace
/**
Example output:
1) /var/www/test/test.php(15): SomeClass->__construct()
2) /var/www/test/SomeClass.class.php(36): SomeClass->callSomething()
**/```