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, and 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 means to use 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. lib_config.php is automatically loaded and executed by default.
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 Use of phar package in PHP. For more information, please follow other related articles on the PHP Chinese website!