In PHP, I have created a user defined function. Example:

<?php
function test($one, $two) {
    // do things
}
?>

I would like to find the names of the function parameters. How would I go about doing this?

This is an example of what I would like:

<?php
function test($one, $two) {
    // do things
}

$params = magic_parameter_finding_function('test');
print_r($params);
?>

This would output:

Array
(
    [0] => one
    [1] => two
)

Also this is very important that I am able to get the user defined function parameter names outside the scope of the function. Thanks in advance!

Comments

Why would you EVER need to do something like this?

Written by Coronatus

@coronatus you and me both are wondering the same thing?

Written by Chris

Smells like en.wikipedia.org/wiki/Inner-platform_effect

Written by Bill Karwin

@Coronatus there are times you may want this. Such as a documentation engine, or a way to map named parameters (to emulate python's functionality, so you could do call_func_custom($callback, array('one' => 'foo', 'two' => 'bar'))... I'm not saying there are a lot of uses, but there definitely are uses...

Written by ircmaxell

Building a documentation generator, building an IoC/DI container... there are reasons. Not clear if the OP actually has a good reason for doing it, though. :)

Written by Beau Simensen

I want to use it so that I can create a menu hook type in Drupal that expects an AJAX call. It will take in a POST of key / values and call the menu callback with it. Using call_user_func_array() the parameters must be ordered. I need it to write code that will order it for you.

Written by Diamonds

Accepted Answer

You can do this using reflection...

$reflector = new ReflectionFunction('test');
$params = array();
foreach ($reflector->getParameters() as $param) {
    $params[] = $param->name;
}
print_r($params);
Written by ircmaxell
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki