Skip to content

Instantly share code, notes, and snippets.

@vaibhav-kaushal
Created July 26, 2018 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vaibhav-kaushal/0cbaccd22494ff0786023349305829c0 to your computer and use it in GitHub Desktop.
Save vaibhav-kaushal/0cbaccd22494ff0786023349305829c0 to your computer and use it in GitHub Desktop.
Create a PHP Phar file

Create a PHP Phar file

Run: php create-phar.php

app.phar successfully created

Start a phar file

Run: php app.phar

App started

<?php
// Filename: app/index.php
// Optional load composer autoloader
//require_once __DIR__ . '/vendor/autoload.php';
echo "App started";
<?php
// better set to php.ini phar.readonly = 0
ini_set("phar.readonly", 0);
$pharFile = 'app.phar';
// clean up
if (file_exists($pharFile)) {
unlink($pharFile);
}
if (file_exists($pharFile . '.gz')) {
unlink($pharFile . '.gz');
}
// create phar
$p = new Phar($pharFile);
// creating our library using whole directory
$p->buildFromDirectory('app/');
// pointing main file which requires all classes
$p->setDefaultStub('index.php', '/index.php');
// plus - compressing it into gzip
$p->compress(Phar::GZ);
echo "$pharFile successfully created";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment