Why Write Command-Line Applications in PHP?

PHP is well-known to programmers as a popular language for developing web applications. But we can also write command-line PHP applications by using its CLI SAPI—the Command-Line Interface Server Application Programming Interface. Let me catch my breath….

In my experience the reaction to the idea of using PHP for a non-web application is, “Why the Hell would you do that? Using language FOO would be much better!” I can understand this reaction. PHP gets enough hate heaped upon it within the sphere of web development. So it is no surprise that people would not think favorably about using PHP for anything else.

Nonetheless, I believe PHP can be the right choice for some command-line applications, programs that are not meant to run on a web server. And today I’ll explain why. And in the process I’ll probably make myself appear insane in the eyes of my peers.

Sometimes You Just Need PHP

I will start with a real-world example. I do all of my programming—really all of writing of any kind—in GNU Emacs. I use the YASnippet package in Emacs to provide convenient short-cuts; it expands snippets into larger chunks of text. So for example, when writing JavaScript I will type gebi and YASnippet expands that into document.getElementById() with my cursor between the parentheses.

YASnippet also lets you create templates with default values, dynamic behavior, some neat stuff. I wanted to take advantage of that to help my PHP programming by having snippets for every function in PHP’s standard library. But I sure as Hell was not going to write all of those templates by hand. So I wrote my own package that dynamically creates snippets for PHP functions on request. An important component of the package is a PHP program that Emacs calls from the command-line to create the snippets. That program accepts the name of a function like json_encode and spits back:

#key: json_encode
#name: json_encode
#group: json
# --
json_encode(${1:$value,} ${2:[$options],} ${3:[$depth],})

The reflection API in PHP is what makes the package feasible, and that gets to the heart of my point: PHP was clearly the best choice for this command-line application. The justification was easy since its purpose is to assist with PHP programming. Some of you may feel like there is too much bias in the premise of this example. “Duh—of course you’d use PHP in a program that works with PHP.”

Correct. But that’s still an example of PHP being the best choice for a command-line program.

Other Reasons

Just because a program is not a web application that does not mean it may not interact with a web service. For example, hub is command-line application to help you use GitHub’s services. It is not a web application but its purpose is to operate with one. One can use the same motivation for writing a non-web application in PHP.

To give another example, I am building a rather simple web application for a client, using PHP. It performs a common task in the web world: it fetches data from one web service, mangles it into a different form, and spits out that newly-processed data on the other side. To help test the application I am also writing a small command-line program that loads the code intended for the web application and performs the data fetching, processing, and so on. The fact is that the language of the web application naturally dictates the language of its command-line companion, but what’s wrong with that? It also gives me the chance to use some nice testing tools.

In Conclusion

You can boil both of my examples down to, “You already needed to use PHP in the first place so obviously that’s why you used it.” And you would not be incorrect. But that admission validates my point that sometimes PHP is the right choice for a command-line application. It sounds like a crazy opinion—sure—but at least other people share my craziness.

Advertisements

4 thoughts on “Why Write Command-Line Applications in PHP?

    1. If you are talking about YASnippet, then yes, it serves a similar purpose as zencoding. But since zencoding is specifically for HTML/CSS markup it’s more efficient to use for that than YASnippet, which is language agnostic and can be used for any major mode (e.g. I use some snippets when writing email).

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s