如何制作可执行的phar?

mag*_*tik 7 php shebang phar

我想直接通过foo.phar <params>代替而开始将phar脚本作为可执行文件php foo.phar <params>.

mag*_*tik 15

通过修改默认存根(添加对应于php的shebang)很容易.

// 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();
Run Code Online (Sandbox Code Playgroud)

  • 由于PHP可执行文件不在所有系统的`/ usr/bin/php`中,因此最好使用`#!/ usr/bin/env php`.见http://tech.vg.no/2012/02/21/dont-hardcode-php-executable-path-in-shebang/ (14认同)
  • 不,在Windows上你应该做一些事情,比如将.phar扩展名与PHP相关联. (2认同)