Search code examples
phpphar

how to convert a php script into a phar file?


The goal of the project is to convert a php script into .phar

My project is made of : 1 single index.php that calls classes into /lib and /database folders

the goal is to have as less files as possible to distribute (ideally 2 : index.php and assets.phar which will include all files from /lib and /database) or even 1 (index.phar)

I tried with empir with any success: has someone has done that before ?

Any tutorial available ?


Solution

  • Use the Phar class. For example, put this in compress.php:

    $phar = new Phar('index.phar');
    $phar->addFile('index.php');
    $phar->buildFromDirectory('lib');
    $phar->buildFromDirectory('database');
    

    Run php compress.php from the command line. Voilà, you have yourself a Phar.