Features Releases

Working with Phar packages in PhpStorm

PHP comes with the Phar extension bundled, which allows us to put entire libraries or applications into a single “phar” file. This makes it easier to distribute our code: it’s just one file! PhpStorm can work with Phar packages in several scenarios: we can browse the contents of a Phar package, use libraries shipped as Phar packages, debug Phar packages and even run PHPUnit from a Phar package!

As an example, let’s use the PHPExcel project. This project offers us a way to work with spreadsheets and export them as Excel, ODF, PDF, … We can make use of this library by downloading it, using the PEAR package, using Composer or… using a Phar package. When we download and add the Phar package to our project, PhpStorm allows us to drill down into the package contents from our project explorer.

We can also write our code and include the Phar package using PHP’s include or require function. PhpStorm will then provide autocompletion support for all classes and functions available from the Phar package.

When setting breakpoints and running code using the debugger, we can step into code that is located inside a Phar package. Note that this only works with the XDebug debugger.

Another popular framework which ships as a Phar package is PHPUnit, the unit testing framework which is also supported by PhpStorm. From the project settings (File | Settings), we can specify how we want to load PHPUnit for running unit tests. We can do this from the include path, from an autoloader file such as Composer) or from a Phar package. PhpStorm can even download the Phar package for us.

As always, we appreciate your feedback in the comments below or in our forums!

Develop with pleasure!
– JetBrains PhpStorm Team

image description