Home >Backend Development >PHP Tutorial >Detailed tutorial on using phar package in php
Phar Introduction:
The concept of Phar archiving comes from Java™ technology's JAR archive, which allows applications to be packaged using a single file that contains all the data needed to run the application. Everything needed. This file differs from a single executable file, which is typically generated by a programming language, such as C, because the file is actually an archive file rather than a compiled application. So the JAR file actually contains the files that make up the application, but these files are not carefully differentiated for security reasons. The Phar extension is based on a similar concept, but is designed primarily for PHP's web environment. Also, unlike JAR archives, Phar archives can be processed by PHP itself, so no additional tools are required to create or use them.
Phar extensions are not a new concept to PHP. It was originally written in PHP and named PHP_Archive before being added to the PEAR library in 2005. In practice, however, pure PHP solutions to this problem were very slow, so in 2007 it was rewritten as a pure C language extension, and support for ArrayAccess Object traversal Phar archives using SPL was added. Since then, a lot of work has been done to improve the performance of Phar archives.
Create Phar
Creating a Phar file requires several steps. All steps require some form of PHP command to complete the creation, as there is no standalone tool for creating archives.
The following article mainly introduces you to the relevant information about the use of phar package in PHP. The introduction in the article is relatively detailed. Friends who need it can take a look below.
Preface
After PHP5.3, a Java-like jar package named phar is supported. Used to package multiple PHP files into one file.
First you need to modify the php.ini configuration to turn off readonly of phar. By default, phar packages cannot be written. include is turned on by default.
phar.readonly => On
Create a phar compressed package
<?php $phar = new Phar('swoole.phar'); $phar->buildFromDirectory(DIR.'/../', '/\.php$/'); $phar->compressFiles(Phar::GZ); $phar->stopBuffering(); $phar->setStub($phar->createDefaultStub('lib_config.php'));
The parameter of new Phar
is the name of the compressed package. buildFromDirectory specifies the compressed directory, and the second parameter can specify the extension of the compressed file through regular rules.
Phar::GZ
indicates using gzip to compress this file. Also supports bz2 compression. Just change the parameter to PHAR::BZ2
.
setSub is used to set the file to start loading. By default, lib_config.php will be loaded automatically and executed.
After executing this code, a swoole.phar file is generated.
Use phar compression package
<?php include 'swoole.phar'; include 'swoole.phar/code/page.php';
Using phar can easily package your code and integrate it for deployment to online machines.
The above is the detailed content of Detailed tutorial on using phar package in php. For more information, please follow other related articles on the PHP Chinese website!