phar extension to save space

小云云
Release: 2023-03-21 18:14:01
Original
1482 people have browsed it

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(&#39;phar://animals.phar/wild.php&#39;);    
 include(&#39;phar://animals.phar/domestic.php&#39;);    
 $test = animal();    printf("%s",$test->get_type);    
 $test1 = new \wild/animal();    
 printf("%s",$test1->get_type());    
 ?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!