PHP – Get methods of a class along with arguments

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

<?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.

Alexa, start a new post called, “I’m blogging this with my voice.”

One of my coworkers recently created an Alexa skill that lets you check interact with your blog.

Check out his original post for more information.

I’ve written and published an Alexa skill that lets you check your blog notifications, moderate comments, and start new draft posts on your WordPress.com or Jetpack-enabled blog, all by speaking to any Alexa-enabled device. The skill is called “Blog Helper”; you’ll find it in the Skills section of your Alexa app or by saying, “Alexa, enable the Blog Helper skill.” After linking your WordPress.com account and choosing the blog you want to access, you can

Source: Alexa, start a new post called, “I’m blogging this with my voice.” | Less Talk, More Do

It’s Time: Apply For Early Access to Your Own .blog Domain Name

I’m super excited to get my .blog domain.

If you’ve been looking for the perfect address for your blog, it’s about to get even easier. Millions of new .blog domain names (like yourname.blog) will be available this November — and starting today, you can apply to secure your own domain name.

Source: It’s Time: Apply For Early Access to Your Own .blog Domain Name — WordPress.com News

Blog for Yourself

Blogging for me has always been a very cyclical thing. The cycle looks something like this:

  • Find something interesting that motivates me to write.
  • After writing that first post, I’ll usually follow up with a few more over the next few weeks.
  • Blog again looks like a ghost town about 2-3 weeks later.

Some of the reasons for this are that I feel like my site has no purpose and that my thoughts don’t matter in the context of the entire Internet. I have found myself thinking that surely there is someone smarter and more eloquent than I am that can, and is, describing any topic that I write about…

This cycle I go through is what interested me in Mike Johnston’s flash talk at the 2014 Grand Meetup in Utah.

I don’t like blogging

Mike’s presentation had to do largely with his recent dislike of personal blogging, which hit home with me because I find that I tend to blog in patterns.

I was currently going through a dislike of blogging myself, which I found odd because I work at Automattic (company behind WordPress.com).

At the grand meetup I brought this subject up to James Huff who gave me a few words of wisdom which he’s used to build his blog to 3,000+ followers. The most impactful thing that James told me was that I should change the way I look at my blog — I should start looking at my blog as something meant for me, such as a way to look back at my life.

This really changed things for me as I could then blog without worrying about whether the message fit into some overall strategy or purpose. From now on, I could simply post things that I thought were interesting, helpful, funny, etc.

The Result

The overall result so far has been a very noticeable increase in how much I post to my blog. You can see this for yourself by comparing the 16 posts I wrote in October to the single post I wrote in June.

In the past month or so, I have blogged about a greater variety of things, including: photos, development tutorials, the death of my daughter, and my work at Automattic.

Photo Credit: Kristina B