开发者

check whether the function needs parameters

for example I need some code like:

if(need_params('function_name')):
    print 'function_name($params)';
else
    print 'function_na开发者_JAVA技巧me()';
endif;


You should have a look into the ReflectionFunction Class.

<?php
function need_params($func) {
    $reflection = new ReflectionFunction($func);

    return $reflection->getNumberOfParameters();
}

// use
function foo($arg) {}

echo need_params('foo') > 0 ? 'Needs params' : 'No params';
?>


You can get the number of function arguments through getNumberOfParamers().

You can check whetther the result is > 0.


You can use reflection to do this:

function need_params($func_name)
{
    $reflect = new ReflectionFunction($func_name);

    return !empty($reflect->getParameters());
}


You should be able to find that out using the Reflection API.


The ReflectionFunctionAbstract::getNumberOfParameters() method looks like exactly what you need, in order to know whether a function expects some parameters.

And you can take a look at the ReflectionFunction for a list of what you can do with it.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜