Search code examples
phpphar

What is $alias in PharData::__construct() for, and how to correctly skip this parameter?


The PharData constructor is declared as such:

PharData::__construct ( string $fname [, int $flags [, string $alias [, int $format = Phar::TAR ]]] )

$alias is documented as:

Alias with which this Phar archive should be referred to in calls to stream functionality.

  • What does this mean, and what would be a use case for this?
  • How to best skip this parameter if I want to define $format, but not $alias? Should I pass an empty string, or NULL?

Solution

  • The $alias parameter is optional. It's intended to allow accessing phar contents later without the full .phar path or filename. Commonly for self-contained PHARs, you might want to map it relocateable as e.g. phar://myphar/script1.php

    You can just pass in NULL or an empty string to skip this parameter. In phar.c the primary test before registering an alias is just the string length:
    https://github.com/php/php-src/blob/master/ext/phar/phar.c#L988