Home  >  Article  >  Backend Development  >  PHP's Phar package turns out to be so powerful

PHP's Phar package turns out to be so powerful

藏色散人
藏色散人forward
2019-11-21 14:33:492868browse

I have seen people say in the group that some Swoole frameworks are slow to start, especially when running on a mounted network hard disk.

I think that since Swoole can make PHP applications resident in memory, many files must be scanned and loaded during startup. This should be the culprit of slow startup speed.

Thinking of php and the phar package, I tried to use phar to solve this problem. The hard work paid off. I finally implemented the compatibility and support of phar packaging for the imi framework. The test results show:

Use Phar to enumerate, read, and include files, whether on a local hard disk or on a network such as Samba. The performance of file sharing mounting is stronger than that of scattered files.

phar is powerful and you can directly use a package to deploy projects, but in traditional PHP application development, phar is actually not popular because it does not have many benefits. In the Swoole environment, more and more frameworks support resident memory, which can load the files you want to use at startup.

phar has built-in support since PHP 5.3.0, so there is no need to worry about compatibility or install additional extensions.

I specially wrote a Demo to test the performance of Phar file enumeration, reading, and include:

https://gitee.com/yurunsoft/PharTest

Interested friends can download it and run it yourself.

To start the Phar file packaging function, you must first configure phar.readonly = Off in php.ini

The Phar packaging code is mainly as follows:

<?php
$fileName = __DIR__ . &#39;/test/test.phar&#39;;
if(is_file($fileName))
{
    unlink($fileName);
}
$phar = new Phar($fileName);
$phar->stopBuffering();
$phar->buildFromDirectory(__DIR__ . &#39;/src&#39;);

The actual application situation is complicated , you can refer to the official PHP documentation and make slight modifications according to the actual situation.

The above is the detailed content of PHP's Phar package turns out to be so powerful. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete