开发者

how to backtrace default function arguments?

开发者 https://www.devze.com 2023-03-16 20:48 出处:网络
function backtrace() { var_dump(debug_backtrace()); } function echosth($what = \'default text\') { echo $what;
function backtrace() {
    var_dump(debug_backtrace());
}


function echosth($what = 'default text') {
    echo $what;
    backtrace();
}

echosth('another text'); //argument is shown 
                         // ["args"]=> array(1) {[0]=>&string(12) "another text"}
echosth(); //it appears as function has none arguments ["args"]=>array(0) {}
开发者_C百科

Is there any way to get to default value of parent function ?


Yes, through the PHP reflection class's getDefaultValue.

function foo($test, $bar = 'baz')
{
    echo $test . $bar;
}

$function = new ReflectionFunction('foo');

foreach ($function->getParameters() as $param) {
    echo 'Name: ' . $param->getName() . PHP_EOL;
    if ($param->isOptional()) {
        echo 'Default value: ' . $param->getDefaultValue() . PHP_EOL;
    }
    echo PHP_EOL;
}

Since you know about debug_backtrace, you should be able to get the name of the calling function and run it through that loop.


Using Reflection

http://php.net/manual/en/reflectionparameter.getdefaultvalue.php

0

精彩评论

暂无评论...
验证码 换一张
取 消