Phar – PHP archiving practice

Tutorials

How to work with zip archives in PHP. Today we will continue PHP lessons. I guess you noticed that due your project become larger – the more and more files it contain. Sometimes it comes to the thousands of files. And then – Phar can help you. This utility allows us to pack a variety of files in the single library file. Thus, we can significantly reduce the number of include files in the project, and work with the entire library as with single file. It is also possible to have a packed (gzip/bzip2) version of the library. These principles already using in other languages, such as a DLL library for system languages (as example .Net: N# and VB.Net) or JAR files for Java. Read more.

Phar extension appear in PHP since version 5.2. And since 5.3 – this is part of PHP core. Firstly I can suggest you to check version of your PHP. And if you have PHP 5.2 – you will need to re-build PHP core with Phar, zlib and bzip2 extensions. If you have 5.3 – just ignore, Phar already installed by default.

Make attention, by default, the PHAR archives have read-only access (so you will unable to create it). So, to have possibility to create own libraries (archives) you should to set phar.readonly = 0 in your php.ini and restart server.

In our lesson, I will tell you how you can create own libraries and use it. Here are samples and downloadable package:

[sociallocker]

download in package

[/sociallocker]


Ok, download the example files and lets start coding !


Conclusion

In result, want to tell that here are several advantages of using Phar: its easy to use, you can pack multiple resource-files in single library, and, it have good performance. If you have any suggestions, or you just have to say – we are glad to hear it. Good luck in your projects!

 

Rate article