Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-settings.php on line 468

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-settings.php on line 483

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-settings.php on line 490

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-settings.php on line 526

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /home/nickawil/websites/nickawilliams/www/wp-includes/theme.php on line 618
NickAWilliams.com » Blog Archive » The New .phar PHP Package

The New .phar PHP Package

September 4th, 2008 by nick

Today I discovered a very powerful addition to the PHP world. Phar is an archive extension for PHP that allows an entire PHP application to be packaged into a single file. It’s basically PHP’s answer to Java’s .jar archive format. Don’t get excited yet, it gets better . . .

There are a few things that make this particularly handy. For one, it is being integrated into the next major PHP release (5.3) wich means that any standard PHP installation running the latest version will support .phar files right out of the box.

The other advantage is of course in deployment. Using a .phar file you can deploy an entire PHP application by working with just one file, rather than a buch of files and directories. This is where .phar really shines. Imagine deploying a popular web app like WordPress by simply copying a single .phar file to your web server’s root, rather than figuring out how to zip it up, upload it, and somehow unzip it remotely. Piece of cake!

The devlopment side is equally as simple, being able to access files within the .phar file as easily as if it were another directory in the filesystem. The .phar file itself can be included in a script using any of PHP’s standard import constructs (include, include_once, require, require_once). This also means external libraries can be more easily integrated, having just a single file to include in order to utilize a given library. A .phar archive can also be accessed as a stream using the same functions used to read/write other types of streams.

// Referencing a .phar archive or its contents:
include 'userlibrary.phar';
include 'phar://userlibrary.phar/internal/file.php';
 
// Utilizing the Phar Stream Wrapper:
header('Content-type: image/jpeg');
echo file_get_contents('phar://userlibrary.phar/img/image.jpg');

Certainly there are disadvantages as well, and this is not a one-size-fits-all solution. Using .phar files does result in a performance hit, meaning traffic-intensive sites and heavyweight applications are probably not the best place to use this tool. It also may not be ideal for a web app that is fairly modular, or other situations where source files might be added or removed on a regular basis. It does have its uses though, and for me it will be a welcome addition to the PHP core.

9 Responses

  1. Tech Weekly วันที่ 6 กันยายน 2551

    [...] The New .phar PHP Package [...]

  2. Vincent

    Can you call a specific file within the Phar archive from HTTP? Is there some kind of URL mechanism?

  3. nick

    From what I’ve been able to find, there isn’t any mention of a way to do that. I’ll update my post if I run across anything though.

    As a workaround, if you’re using Apache you could do a mod_rewrite and watch for URLs with .phar in them. Then you could pass the URL contents to a PHP script that could in-turn open the file for you.

  4. Pablo

    Acctually, the performance is pretty good, there’s no big impact, and you can use with APC to do some code cache.

    BTW, the PEAR package now comes as a Phar.

    Regarding the the phar over http, it’s very easy, just add the application type to your apache configuration, the phars are designed to run out of the box, more if you use Phar::webphar on your stub.

  5. nick

    That’s great to hear! I’ve been thinking about using this package in my next project, and it’s sounding more and more viable given how flexible it seems to be.

  6. Nick Williams’ Blog: The New .phar PHP Package : Dragonfly Networks

    [...] a quick post to his blog, Nick Williams points out a very handy extension for PHP - [...]

  7. Pablo Viquez

    I did a presentation about PHAR for the office. I made available my slides here: http://www.pabloviquez.com/2008/09/phar-files/

    In case you’re interested.

  8. Lee

    As Pablo mentiond, you can call Phar::webPhar(), Phar::mungServer and Phar::interceptFileFuncs() to make a .phar a complete web application.

    Phar::webPhar() actually installs a front controller into the .phar that makes it painless.

    Phar::mungServer() fixes up the $_SERVER global

    Phar::interceptFileFuncs() will make relative references inside the .phar properly point back into the .phar

  9. Lenguajes X » Simular paquetes en PHP

    [...] script que nos permite cargar paquetes al estilo Java desde PHP. Aunque ya existen los phar, que es un paquete de PHP, esto puede resultar una posibilidad o al menos un método a tener en [...]

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.