1

I have a file which consist of user defined functions with CamelCase names :

// some useful comments
function functionOne(){
   return false;
}

// some other useful comment
function addTwoNumbers($x,$y)
{
   return 5;
}
...

I would like to output these functions sorted by name like

addTwoNumbers($x,$y) 
2 parameters 
Comment: some other useful comment

functionOne() 
0 parameters 
Comment: some useful comments

So far, I get a list of function names like this:

include '../includes/functions.php';

$allFunctions = get_defined_functions();

$userFunctions = $allFunctions['user'];
sort($userFunctions);

foreach ($userFunctions as $functionName) {
  echo $functionName. "<br>";
}

My main problem is that I do not know how to display the number of parameters of each function and the parameter variable names.

Further (but I could live with it) the function names are only displayed with lower case letters, so I can't read them in CamelCase.

And finally the comments are of course not shown. I was thinking of writing them in an array $comments['functionOne']="some useful comments".

So my main problem is how do you get the parameter variable names and number.

4
  • so, please don't make your function name camel case then. PHP is not Java. Commented Jun 2, 2016 at 9:06
  • As a start, check the reflection api. But why would you want to do this? You could simply use phpDocumentor. Commented Jun 2, 2016 at 9:17
  • @Raptor I don't think that is enough reason to not use camelCase. Many modern frameworks and libraries like Zend Framework 2 and Symfony 2 use camelCase for method names. And I personally like it. Commented Jun 2, 2016 at 10:05
  • @Yoshi thank you! Looks awesome, I will give it a try. Commented Jun 2, 2016 at 10:05

2 Answers 2

3

To get paramater number and names, you can use Reflection in PHP :

function getFunctionArgumentNames($functionName) {
    $reflection = new ReflectionFunction($functionName);
    $argumentNames= array();
    foreach ($reflection ->getParameters() as $parameter) {
        $argumentNames[] = $parameter->name;   
    }
    return $argumentNames;
}

Then you'll have your parameter names, and the length of the returned array will give you how many there are.


More information can be found about it :

  • ReflectionClass
  • ReflectionObject
  • What is Reflection in PHP ?

    Another common use of Reflection is for creating documentation. It would be extremely labour intensive to write documentation about every method of every class of a large framework or application. Instead, Reflection can automatically generate the documentation for you. It does this by inspecting each method, constructor and class to determine what goes in and what comes out.

Sign up to request clarification or add additional context in comments.

Comments

0

Use ReflectionFunction class

for example:

$refFunc = new ReflectionFunction('preg_replace');
foreach( $refFunc->getParameters() as $param ){
    print $param;
}

The ReflectionFunction class reports information about a function. See more http://php.net/manual/en/class.reflectionfunction.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.