Handy PHP Functions when Working with PHAR Archives

If yu have been in PHP long enough you will have heard about PHAR. If you have not the PHAR is an acronym for PHP Archive. It allowes to put entire PHP applications into a single ".phar" file. This is quite handy for lots of different situations.

If you need to work with creating PHAR files, here are some handy functions to get you going:

1. Determine if PHP is Running from a PHAR Archive

function isPhar() {
  return strlen(Phar::running()) > 0 ? true : false;

2. Determine the Running Directory of the PHAR file

function getRunningDir() {
  if(strlen(Phar::running()) > 0){
    $pharDir = Phar::running(false);
    return dirname($pharDir);
  return __DIR__;

Happy PHAR-ing :)