I'm trying to create a very simple PHP CLI application that can be run as a phar file from the command line:
# php myProject.phar
This is what I've tried so far:
My Project
My project is in a directory called MyProject
and it has these two files in it:
|-- createPhar.php
`-- bootstrap.php
bootstrap.php
The bootstrap.php
file contains this:
<?php
print phpversion() . PHP_EOL;
print 'i am some script' . PHP_EOL;
When I run this script from my Ubuntu command line:
# cd MyProject
# php bootstrap.php
I get the following output:
5.3.2-1ubuntu4.9
i am some script
createPhar.php
The createPhar.php file is meant to turn the project into Phar archive. It looks like this:
<?php
$phar = new Phar('MyProject.phar');
$phar->addFile('bootstrap.php');
$phar->setStub( $phar->createDefaultStub('bootstrap.php') );
When I run that script...
# php createPhar.php
... a new file called MyProject.phar
is created in my project's directory.
|-- bootstrap.php
|-- createPhar.php
`-- MyProject.phar
Now here's the problem
When I run the phar file...
# php MyProject.phar
...I expect to see the same the same output that I got when when I ran the bootstrap.php
script.
Instead I see nothing. No output at all. This implies that my bootstrap.php
script is not being included by the default stub that was created by $phar->createDefaultStub('bootstrap.php')
I think I am misunderstanding how Phars and their stubs are being created. Could you, please, explain where I have gone wrong.
->setDefaultStub
shortcut (over ::createDefault and ::setStub) for testing. – mario->setDefaultStub
and it still did not work either. Its weird that you got it to work and I haven't. I suppose the problem is not to do with the code then. – JW.PHP_VERSION
is5.3.3-1ubuntu9.5
, but there was only one minor security bug fix related to the phar extension in between. It must be something else. Did you look at the resulting file with a hexeditor? Is the bootstrap content anywhere in it? (I got a plain php script with some admixed binary gibberish.) – marioMyProject.phar
and there is bootstrap code plus some gibberish in it. Thanks for your suggestions. I suspect I am doing something really stupid somewhere along the line - so gonna sleep on it and re-try tomorrow. – JW.