I want to start a phar script as an executable, directly by doing foo.phar <params>
instead of php foo.phar <params>
.
Asked
Active
Viewed 8,355 times
1 Answers
18
It's easy by modifying the default stub (adding the shebang corresponding to 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();

magnetik
- 4,351
- 41
- 58
-
16Since the PHP executable is not in `/usr/bin/php` on all systems, it's better to use `#!/usr/bin/env php` instead. See http://tech.vg.no/2012/02/21/dont-hardcode-php-executable-path-in-shebang/ – chiborg Jul 04 '12 at 13:59
-
2nope, on windows you should do something like associate the .phar extension with PHP. – magnetik May 03 '13 at 14:36
-
Getting a fully functional phar took me quite some time, I hope it can spare you some time https://github.com/8ctopus/webp8/blob/master/src/Compiler.php – 8ctopus Jul 13 '20 at 05:01