In this tutorial, I will explain how to create and use a Phar archive file in PHP your application. Phar archives are similar as JAR archives in JAVA. It is tailored to the flexibility of PHP applications.
The Phar archive is used to create a library from a bundle of PHP codes and it is also used to distribute a complete application or library in a single file.
If you have a bundle of PHP codes or PHP library and you want to create a Phar archive, then follow the below steps:
Step 1) Open php.ini file and search the “;phar.readonly” directive, and replace it with “phar.readonly = 0”. After this restart your web server. Now you are ready to create a Phar file of your application OR Library folders.
Step 2) Create a folder named “sample” and create an index.php file inside that folder. Now copy paste below code in index.php file and open your web browser to create a project.Phar archive.
* Description: Create with alias "project.phar"
* @author: Prem Tiwari
$phar = new Phar('project.phar', 0, 'project.phar');
//add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
Now you can use your Phar archive in any application. Phar archive supports read and writes operations on both files and directories/subdirectories. Below is the example how to use Phar archive in your PHP page.
// phars can be accessed by full path or by alias
The Phar archive will save you from lots of hassle in packaging and deploy your web applications. I recommend you to using it in your web applications. Do you like & share this article with your friends, and don’t forget to follow us on Facebook and Twitter to learn cool tutorials.