How to make phar executable?

I want to run phar script as an executable by executing foo.phar <params>instead php foo.phar <params>.

+5
source share
1 answer

Easy by changing the default value (by adding a shebang matching php).

// start buffering. Mandatory to modify stub.
$phar->startBuffering();

// Get the default stub. You can create your own if you have specific needs
$defaultStub = $phar->createDefaultStub('index.php');

// Adding files
$phar->buildFromDirectory(__DIR__, '/\.php$/');

// Create a custom stub to add the shebang
$stub = "#!/usr/bin/php \n".$defaultStub;

// Add the stub
$phar->setStub($stub);

$phar->stopBuffering();
+12
source

All Articles