किसी दिए गए फ़ंक्शन में कॉलर फ़ंक्शन का नाम जानने के लिए एक PHP फ़ंक्शन है?
किसी दिए गए फ़ंक्शन में कॉलर फ़ंक्शन का नाम जानने के लिए एक PHP फ़ंक्शन है?
जवाबों:
डीबग_बैकट्रेस देखें - यह आपके कॉल स्टैक को शीर्ष पर जाने के सभी तरीकों का पता लगा सकता है।
यहां बताया गया है कि आप अपना कॉलर कैसे प्राप्त करेंगे:
$trace = debug_backtrace();
$caller = $trace[1];
echo "Called by {$caller['function']}";
if (isset($caller['class']))
echo " in {$caller['class']}";
list(, $caller) = debug_backtrace(false);कॉलर प्राप्त करने के लिए उपयोग करें false;-) (php5.3)
echo 'called by '.$trace[0]['function']
debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];बेहतर प्रदर्शन के साथ कॉलर का नाम पाने के लिए।
Xdebug कुछ अच्छे कार्य प्रदान करता है।
<?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();
?>
ट्रेस लौटेगा
callee() called @ /var/www/xd.php: 16 from MyClass::__construct
Ubuntu पर Xdebug स्थापित करने का सबसे अच्छा तरीका है
sudo aptitude install php5-xdebug
आपको पहले php5-dev स्थापित करने की आवश्यकता हो सकती है
sudo aptitude install php5-dev
यह बहुत देर हो चुकी है, लेकिन मैं उस फ़ंक्शन को साझा करना चाहूंगा जो फ़ंक्शन का नाम देगा जिसमें से वर्तमान फ़ंक्शन कहा जाता है।
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;
}
मुझे उम्मीद है कि यह उपयोगी होगा।
debug_backtrace() वर्तमान कॉल स्टैक में मापदंडों, फ़ंक्शन / विधि कॉल का विवरण प्रदान करता है।
echo debug_backtrace()[1]['function'];
PHP 5.4 के बाद से काम करता है ।
या अनुकूलित (गैर-डीबग उपयोग मामलों के लिए):
echo debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
पहला तर्क अप्रयुक्त फ़ंक्शन तर्कों को आबाद करने से रोकता है, दूसरा ट्रेस को दो स्तरों तक सीमित करता है (हमें दूसरे की आवश्यकता है)।
इसे बनाया और खुद इस का उपयोग किया
/**
* 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;
}
}
मैं बस यह बताना चाहता था कि फ्लोरी का तरीका एक फ़ंक्शन के रूप में काम नहीं करेगा क्योंकि यह हमेशा कॉल करने वाले के बजाय कॉल किए गए फ़ंक्शन नाम को लौटाएगा, लेकिन मेरे पास टिप्पणी करने के लिए प्रतिष्ठा नहीं है। मैंने फ्लॉरी के जवाब के आधार पर एक बहुत ही सरल कार्य किया जो मेरे मामले के लिए ठीक काम करता है:
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..
}
आप इस जानकारी को debug_backtrace द्वारा दिए गए सरणी से निकाल सकते हैं
इसने मेरे लिए सबसे अच्छा काम किया: var_dump(debug_backtrace());
वास्तव में मुझे लगता है कि debug_print_backtrace () वह करता है जो आपको चाहिए। http://php.net/manual/en/function.debug-print-backtrace.php
यह यह अच्छी तरह से करेंगे:
// 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()
**/```