A PHAR (PHP Archive) file is a package format that bundles multiple PHP code files and resources like images and stylesheets into a single archive for easy distribution. PHAR files use the .phar file extension and come in three formats: tar, ZIP, and a custom PHAR format. Tar and ZIP archives are compatible with standard utilities, while the PHAR format requires PHP-specific tools such as the PHAR extension for PHP or the PEAR PHP_Archive package to create or unpack them.
History
Created in 2004, PHAR files were influenced1 by Java's JAR file format with the aim of speeding up deployment of applications via FTP.2 Rather than using the ZIP file format the simpler tar format was used, and the first PHAR was created using the tar command line utility3 as a proof of concept. On June 14, 20044 the initial PHP_Archive package was proposed to PEAR and on December 13, 2004 it was accepted unanimously.5
PHAR Extension
In 2007, PHP_Archive was ported to C and released as a PECL extension.6 The extension was included and enabled by default in PHP 5.3.0.78
Design
A PHAR file allows for a Tar, Zip or PHAR formatted archive. Regardless of format, each archive contains three sections:
- Stub — A PHP file that will bootstrap the archive. The stub must contain the __HALT_COMPILER(); token, and the default stub includes the ability to run a PHAR with or without the PHP extension enabled9
- Manifest — The manifest details the contents of the archive
- File Contents — The original files that are included in the archive
Additionally, the PHAR format may also include a signature for verifying PHAR integrity.
Manifest
The manifest contains meta-data information about the archive, and its contents. The binary format is intended to be efficient to parse in both PHP and C. It consists of fixed length segments, in addition to pairs of length specifications followed by variable length segments.10 Each file has its own manifest within a segment of the global manifest. The current format is version 1.1.1.
Compression
All three formats have support for compression, however only the PHAR format supports both per-file and whole archive compression. Zip and Tar formats only support per-file and whole file compression respectively.
Executing PHAR files
Assuming the PHAR extension is enabled, all PHAR files may be executed simply by executing them with the PHP interpreter ("php file.phar"). If the PHAR extension is not enabled, only PHAR format can be executed.11
Additionally, it is possible to mark a PHAR file as executable, and to add an appropriate shebang to make the PHAR executable directly.
Usage
While PHAR was originally intended for web usage, it is often used by command line utilities. Popular applications distributed in PHAR format, include Composer and PHPUnit.
External links
References
"PHP 5.3 5th Anniversary: The History of PHP Archives (PHAR Files)". blog.engineyard.com. Archived from the original on 2017-09-17. Retrieved 2016-06-16. https://web.archive.org/web/20170917124023/https://blog.engineyard.com/2014/php-53-5th-anniversary ↩
"PHP 5.3 5th Anniversary: The History of PHP Archives (PHAR Files)". blog.engineyard.com. Archived from the original on 2017-09-17. Retrieved 2016-06-16. https://web.archive.org/web/20170917124023/https://blog.engineyard.com/2014/php-53-5th-anniversary ↩
"PHP 5.3 5th Anniversary: The History of PHP Archives (PHAR Files)". blog.engineyard.com. Archived from the original on 2017-09-17. Retrieved 2016-06-16. https://web.archive.org/web/20170917124023/https://blog.engineyard.com/2014/php-53-5th-anniversary ↩
"PEPr :: Details :: PHP_Archive". pear.php.net. Retrieved 2016-06-16. http://pear.php.net/pepr/pepr-proposal-show.php?id=88 ↩
"PEPr :: Votes :: PHP_Archive". pear.php.net. Retrieved 2016-06-16. http://pear.php.net/pepr/pepr-votes-show.php?id=88 ↩
"PECL :: Package :: phar". pecl.php.net. Retrieved 2016-06-16. https://pecl.php.net/package/phar ↩
"PHP: News Archive - 2009". php.net. Retrieved 2016-06-16. http://php.net/archive/2009.php#id2009-06-30-1 ↩
"PHP: PHP 5 ChangeLog". php.net. Retrieved 2016-06-16. http://php.net/ChangeLog-5.php#5.3.0 ↩
"PHP: Phar::createDefaultStub - Manual". php.net. Retrieved 2016-06-17. http://php.net/manual/en/phar.createdefaultstub.php ↩
"PHP: Phar File Format - Manual". php.net. Retrieved 2016-06-17. http://php.net/manual/en/phar.fileformat.phar.php ↩
Mertic, John (January 27, 2009). "What's new in PHP V5.3, Part 4, Creating and using Phar archives". IBM.com. Archived from the original on March 2, 2020. Retrieved March 3, 2020. https://web.archive.org/web/20200302205407/https://www.ibm.com/developerworks/library/os-php-5.3new4/index.html ↩