Home > Article > Backend Development > How to use the archive format phar for PHP development
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 ArrayAccess Object traversal Phar 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:
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:23 */ function show(){ echo "l am show()"; }
msg The content of .html is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>phar</title> </head> <body> <?=$str; ?> </body> </html>
The content of index.php is as follows:
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:17 */ require "lib/lib_a.php"; show(); $str = isset($_GET["str"]) ? $_GET["str"] : "hello world"; include "template/msg.html";
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:20 */ function yunke() { echo "l am yunke()"; }
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:36 */ //产生一个yunke.phar文件 $phar = new Phar('yunke.phar', 0, 'yunke.phar'); // 添加project里面的所有文件到yunke.phar归档文件 $phar->buildFromDirectory(dirname(FILE) . '/project'); //设置执行时的入口文件,第一个用于命令行,第二个用于浏览器访问,这里都设置为index.php $phar->setDefaultStub('index.php', 'index.php');
project yunkeBuild.php yunke.phar
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/8 * Time: 9:33 */ require "yunke.phar"; require "phar://yunke.phar/Lib.php"; yunke();
require "project/index.php";
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/9 * Time: 19:02 */ $phar = new Phar('composer.phar'); $phar->extractTo('composer'); //提取一份原项目文件 $phar->convertToData(Phar::ZIP); //另外再提取一份,和上行二选一即可
$phar = new Phar('lib/yunke.phar', 0); $phar->setAlias ( "yun.phar");
<?php require "lib/yunke.phar"; require "phar://yun.phar/Lib.php"; //使用别名访问归档文件 require "phar://lib/yunke.phar/Lib.php"; //当然仍然可以使用这样的方式去引用
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!