开发者

how to backtrace default function arguments?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜