Lately, I’ve been using the command line a lot more often at work. I found two things hard about using the command line to interact with PHP files:

  1. Figuring out the require path every time I opened an interactive shell
  2. Remember what methods were available in a class and what arguments the method expected

The first was pretty easy to handle by writing a function that would require often used files. The second one turned out to not be too hard and is the subject of this post.

The code

Below is the code that I used to get the methods of an object as well as the arguments for each method.

<?php

function print_object_methods( $mgr ) {
    foreach ( get_class_methods( $mgr ) as $method ) {
        echo $method;
        $r = new ReflectionMethod( $mgr, $method );
        $params = $r->getParameters();
        if ( ! empty( $params ) ) {
            $param_names = array();
            foreach ( $params as $param ) {
                $param_names[] = sprintf( '$%s', $param->getName() );
            }
            echo sprintf( '( %s )', implode(', ', $param_names ) );
        }
        echo "\n";
    }
}

An example

Let’s use the Jetpack_Options class from Jetpack as an example. You can find it here:

https://github.com/Automattic/jetpack/blob/master/class.jetpack-options.php

For that class, the above code would output:

get_option_names( $type )
is_valid( $name, $group )
is_network_option( $option_name )
get_option( $name, $default )
get_option_and_ensure_autoload( $name, $default )
update_option( $name, $value, $autoload )
update_options( $array )
delete_option( $names )
delete_raw_option( $name )
update_raw_option( $name, $value, $autoload )
get_raw_option( $name, $default )

As a note, in this case, it could also be nice to print out the docblock for each method instead of just the arguments to add some context. But, I didn’t need too much context for a file that I’m in pretty often. Your mileage may vary.

One thought on “PHP – Get methods of a class along with arguments

Leave a Reply

Your email address will not be published. Required fields are marked *