Phars and distributing PHP CLI apps

Published: July 25, 2017

Tags:

First things first, let’s address a question that I couldn’t easily find an answer to on Google. Is PHP required to execute a phar?

The answer is a loud and clear yes.

In fact, not only is PHP required to execute a phar, but the version of PHP installed on the system that will execute the phar must be in line with the version of PHP used by the phar author.

This is due to the nature of what a phar is. A phar is not a compiled standalone binary, like you’d generate with gcc or go build. Instead it’s a way to bundle an entire project into a single standalone package for easy distribution.

For example, if we look at the first few lines of the composer.phar we’ll see the following…

$ head /usr/local/bin/composer
#!/usr/bin/env php
<?php
/*
 * This file is part of Composer.
 *
 * (c) Nils Adermann <[email protected]>
 *     Jordi Boggiano <[email protected]>
 *
 * For the full copyright and license information, please view
 * the license that is located at the bottom of this file.
$

As we can see it’s just a bunch of PHP with a shebang at the top instructing the shell to use PHP to execute the file.

While phars, are a handy way to distribute CLI tools for PHP developers such as composer, PHPUnit, or n98-magerun unfortunately, they’re not a great option for publishing software which may be used outside to PHP community. For example I started writing a Symfony Console based security checking tool which I was thinking about distributing as a phar. However, after spending a bit more time understanding what phars actually are, I’ve begun to question this choice as I don’t want to make having PHP installed a requirement for using the tool.

And even with tools targeting PHP developers, distributing phars typically prevents authors from using new PHP features in order to support users with older versions of PHP installed. For example, n98-magerun still supports PHP 5.3.

Phar alternatives

So what are the alternatives for packaging and delivering software written in PHP for users to execute themselves. Unfortunately the options are not great…

Phalanger is the only thing that is appears to be actively maintained. However, it converts PHP to .NET, not exactly what I’m looking for.

To quickly run through the other projects listed..

Facebook’s HipHop

This was discontinued in 2013 in favor of HHVM, which is not a compiler.

Roadsend

This project has not been updated since 2012.

phc

This project is officially unmaintained

While I’d love to be wrong about this (please tell me I am), there don’t seem to be any good options for packaging and distributing CLI tools written in PHP to users who may not have PHP installed. As such, it’s looking like I’ll need to start over writing the project in go :disappointed:

Max Chadwick Hi, I'm Max!

I'm a software developer who mainly works in PHP, but loves dabbling in other languages like Go and Ruby. Technical topics that interest me are monitoring, security and performance. I'm also a stickler for good documentation and clear technical writing.

During the day I lead a team of developers and solve challenging technical problems at Rightpoint where I mainly work with the Magento platform. I've also spoken at a number of events.

In my spare time I blog about tech, work on open source and participate in bug bounty programs.

If you'd like to get in contact, you can find me on Twitter and LinkedIn.