Executing .phar Files on Windows: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:PHP Category:Configuration Category:Windows == Overview == `.phar` files are "PHP archives" which allows you to execute a PHP script from the command lin...")
 
(No difference)

Latest revision as of 13:21, 3 March 2015

Overview[edit]

.phar files are "PHP archives" which allows you to execute a PHP script from the command line.

On Linux systems this can be done by referencing the archive name minus the .phar extension.

On Windows, you can run it with PHP, e.g. php myphar.phar.

In order to run the .phar file as if it were a stand-alone, create a batch file for it.

File system location[edit]

The convention (based on Composer and PHPUnit) is to save the .phar files in c:\bin (and add that directory to the PATH environment variable).

Creating a batch file[edit]

First, the wrong way to do it. It's perplexing that the installation instructions for PHPUnit are wrong. They say to create the batch file like this:

echo @php "%~dp0phpunit.phar" %* > phpunit.bat

Now the right way (in powershell, at least).

echo '@php "%~dp0phpunit.phar" %* ' > phpunit.bat
  • Make sure there is a space after %*!
  • The .bat file can be saved anywhere that's accessible via the PATH environment variable, but it makes the most sense to save it alongside the .phar file in c:\bin so it's obvious that the corresponding batch file exists, and so it can be cleaned up along with the .phar file if that one ever needs to be deleted.