Let me introduce to you the concept and usage of phar files developed by PHP. We know that a PHP application is often composed of multiple files. It is very convenient if they can be concentrated into one file for distribution and operation. Examples like this There are many, but they are mainly designed for PHP's Web environment. Unlike JAR archives, Phar archives can be processed by PHP itself, so there is no need to use additional tools to create or use it. It can be created or extracted using a PHP script. phar is a compound word composed of PHP and Archive. It can be seen that it means PHP archive file.
Phar archive files come in three formats: tar archive, zip archive, and phar archive. The first two types of execution require Phar to install Phar extension support, and are rarely used. Here we mainly talk about the phar archive format.
Phar format archive files can be executed directly. Its generation relies on the Phar extension and is generated by a php script written by yourself.
Phar extension is not a new concept to PHP. It has been built into php in php5.3. It was originally written in PHP and named PHP_Archive, and then was added to the PEAR library in 2005. . Since pure PHP solutions to this problem were very slow in practice, it was rewritten in 2007 as a pure C language extension, while adding support for ArrayAccessObject traversalPhar archives using SPL. Since then, a lot of work has been done to improve the performance of Phar archives.
Phar extension depends on the php stream wrapper. For this, please refer to the previous article PHP Streams, wrapper wrapper concepts and usage examples
Many php applications are distributed in phar format And running, the famous dependency management: composer, unit testing: phpunit, let's take a look at how to create, run, extract and restore.
Creation of phar file:
First modify the phar.readonly option in php.ini, remove the preceding semicolon, and change the value to off. Due to security reasons, this option defaults to on , if it is disabled in php.ini (the value is 0 or off), then it can be turned on or off in the user script. If it is turned on in php.ini, then the user script cannot be turned off, so it is set here to off to show examples.
Let's create a project. Create the project folder in the server root directory as project. The structure in the directory is as follows:
file -yunek.js -yunke.css lib -lib_a.php template -msg.html index.php Lib.php
The file folder There are two js and css files with empty content. It only demonstrates that phar can contain multiple file formats
lib_a.php content is as follows:
Copy after login
msg The content of .html is as follows:
The content of index.php is as follows:
Copy after login
Copy after login
buildFromDirectory(dirname(FILE) . '/project'); //设置执行时的入口文件,第一个用于命令行,第二个用于浏览器访问,这里都设置为index.php $phar->setDefaultStub('index.php', 'index.php');
project yunkeBuild.php yunke.phar
Copy after login
require "project/index.php";
extractTo('composer'); //提取一份原项目文件 $phar->convertToData(Phar::ZIP); //另外再提取一份,和上行二选一即可
$phar = new Phar('lib/yunke.phar', 0); $phar->setAlias ( "yun.phar");
Copy after login
If you do not specify an alias when making a phar file, you can also use Phar::mapPhar('yunke.phar'); to specify it in the stub file.
3. There is a stub file in the archive file. In fact, It is a piece of PHP execution code that can be set when making an archive. When the archive file is executed directly, it is actually executed, so it is a startup file; when the archive file is included in the script, it is included and run just like a normal PHP file, but When a file in the archive is directly included in phar://, the stub code will not be executed. The stub file often requires other files to be run. The restriction on the stub file is only to end with HALT_COMPILER();, which is the default The stub is designed to run without the phar extension. It extracts the contents of the phar file to a temporary directory and then executes it. However, starting from php5.3, the extension is built-in enabled by default
4. The created phar file cannot be Change, so files such as configuration files need to be placed outside the archive file.
5. mapPhar function: This function should only be called in the stub code. It can be used to set the alias when the archive alias is not set. , open a reference mapped to the phar stream
I believe you have mastered the method after reading these cases. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Related reading:
How PHP solves the problem of large website traffic and high concurrency
AJAX principles and CORS cross-domain methods
Detailed explanation of javascript data types and git usage code
The above is the detailed content of How to use the archive format phar for PHP development. For more information, please follow other related articles on the PHP Chinese website!