1

I would like to have access to the methods by ReflectionMethod:

$r = new ReflectionMethod($class, $method);
$params = $r->getParameters();

and if parameters are required ($Option), get source of that method to prepare the parameters:

public function GetFeedback($Options) {
    $this->checkConnection();
    return $this->_client->doGetFeedback(
        $Options['feedback-from'],
        $Options['feedback-to']
    );
}

so in my example to find $Option keys - 'feedback-from' and 'feedback-to'

3
  • Curious why do you need that? o_O Commented Feb 29, 2012 at 9:52
  • I can call all the methods inside a class with jQuery, and if parameters are required open additional form to include them in request Commented Feb 29, 2012 at 9:56
  • It is not clear what actually you're doing there, but what is clear is that you're doing it terribly wrong Commented Feb 29, 2012 at 9:57

1 Answer 1

4

Assuming a class like

class Foo
{
    public function GetFeedback($Options) {
        $this->checkConnection();
        return $this->_client->doGetFeedback(
            $Options['feedback-from'],
            $Options['feedback-to']
        );
    }
}

this code will do what you ask for

$reflector = new ReflectionMethod('Foo', 'GetFeedback');
$methodBody = implode(
    '',
    iterator_to_array(
        new LimitIterator(
            new SplFileObject($reflector->getFileName()),
            $reflector->getStartLine(),
            $reflector->getEndLine() - $reflector->getStartLine()
        )
    )
);
foreach ($reflector->getParameters() as $parameter) {
    if (!$parameter->isOptional()) {
        preg_match_all(
            sprintf('{\$%s\[[\'"](.*)[\'"]\]}', $parameter->getName()),
            $methodBody,
            $matches
        );
    }
    print_r($matches);
}

Output:

Array
(
    [0] => Array
        (
            [0] => $Options['feedback-from']
            [1] => $Options['feedback-to']
        )

    [1] => Array
        (
            [0] => feedback-from
            [1] => feedback-to
        )
)

However, the only valid usage for this approach IMO is to generate a docblock listing these options and agree that you are likely doing it wrong if you need this in production code.

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

1 Comment

new LimitIterator( new SplFileObject($reflector->getFileName()), $reflector->getStartLine(), $reflector->getEndLine() - $reflector->getStartLine()

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.