Home > Article > Backend Development > phar extension to save space
After PHP5.3, PHP's phar extension can also implement file sharing functions such as java. It allows developers to create or manipulate PHP archive files, which is where the name comes from - PHP archive. For example, in the code below, it contains two files: wild.php and domestic.php. In order to distribute the application, 3 files need to be distributed. If there are more classes, the number of files to be distributed is larger. The purpose of distributing only two files is: the script itself, and the phar file containing all necessary class files.
<?php include('phar://animals.phar/wild.php'); include('phar://animals.phar/domestic.php'); $test = animal(); printf("%s",$test->get_type); $test1 = new \wild/animal(); printf("%s",$test1->get_type()); ?>
The above code lies in the include directive, which introduces the animals.phar file and all references to these files.
Creating a phar file is very simple, the syntax is as follows:
phar pack -f animals.phar -c gzwild.phpdomestic.php
The pack parameter specifies what the phar program is used to do Create a compressed archive with the file name specified with the -f option, and add the two files wild.php and domestic.php to the compressed archive. In order to run successfully, the phar.readonly parameter in the php.ini configuration file needs to be off. If the default value is on, it will prevent the creation of new files.
The compression algorithms supported by phar include zip, gz(gzip), and bz2(bzip2). phar changes the way PHP applications are distributed and packaged, and saves storage space. Like Java's jar package, there is no need to worry about performance issues. The phar package can only be parsed once, occupying a very small amount at the beginning of the script and not affecting the execution time.
Related recommendations:
How to use the archive format phar for PHP development
How to use the phar package PHP
PHP archive phar performance test
The above is the detailed content of phar extension to save space. For more information, please follow other related articles on the PHP Chinese website!