Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check whether the function needs parameters

Tags:

php

for example I need some code like:

if(need_params('function_name')):
    print 'function_name($params)';
else
    print 'function_name()';
endif;
like image 949
Mohammad Ali Akbari Avatar asked Oct 27 '25 08:10

Mohammad Ali Akbari


2 Answers

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';
?>
like image 105
mAu Avatar answered Oct 29 '25 22:10

mAu


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

You can check whetther the result is > 0.

like image 43
marc Avatar answered Oct 29 '25 22:10

marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!