Home >Backend Development >PHP Tutorial >PHP development archive format phar file concept and usage
A PHP application is often composed of multiple files. It is very convenient if they can be concentrated into one file to distribute and run. There are many such examples, such as the installation program on the window operating system, a jquery library, etc. In order to achieve this, PHP uses the phar document file format. This concept is derived from the jar of java, but it is mainly designed for
PHP's web environment, unlike JAR archives, Phar archives can be
Handled natively, so no additional tools are needed to create or use it, just use a php script to create or extract it. phar is a compound word composed of PHP and
Archive composition, you can see that it means php archive file.
There are three formats of phar archive files: 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.
The phar format archive file can be executed directly. Its generation relies on the Phar extension and is generated by a php script written by yourself.
The 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 was then added to the PEAR library in 2005. Since pure PHP solutions to this problem are very slow in practice, 2007 Rewritten as a pure C language extension and added ArrayAccess object traversal using SPL Phar Archiving support. Since then, a lot of work has been done to improve the performance of Phar archives.
The Phar extension depends on the PHP stream wrapper. For this, please refer to the previous article PHP stream Streams, wrapper wrapper concepts and usage examples
Many PHP applications are distributed and run in phar format. The famous ones include dependency management: composer and 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, the user script cannot be turned off, so it is set to off here to show the example.
Let's create a project. Create a 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 has two js and css files with empty contents. It only demonstrates that phar can contain multiple file formats
The content of lib_a.php is as follows:
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:23 */ function show(){ echo "l am show()"; }
The content of msg.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";
The content of Lib.php is as follows:
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/10 * Time: 9:20 */ function yunke() { echo "l am yunke()"; }
The project file is ready and started to be created. Now create a yunkeBuild.php in the same directory as the project folder to generate a phar format file. The content is as follows:
<?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');
Then access this yunkeBuild.php file in the browser, a yunke.phar file will be generated. At this time, the server root directory structure is as follows:
project yunkeBuild.php yunke.phar
This is the simplest process to generate a phar archive file. For more information, please see the official website. It should be noted here that if the project does not have a single execution entry, it is not appropriate to use the phar archive file
Usage of phar archive files:
We create an index.php file in the server root directory to demonstrate how to use the phar file created above. The content is as follows:
<?php /** * Created by yunke. * User: yunke * Date: 2017/2/8 * Time: 9:33 */ require "yunke.phar"; require "phar://yunke.phar/Lib.php"; yunke();
If there is only the first line in the index.php file, then add the following code exactly the same as when not using an archive file:
require "project/index.php";
If there is no second line, then yunke() on the third line will prompt that it is undefined, so it can be seen that when requiring a phar file, not all the files in it are imported, but only the entry execution file is imported. However, in actual projects, it is often Import other files that need to be used in this entry file. In this example, the entry execution file is project/index.php
Extraction and restoration of phar files:
Sometimes we are curious about the source code of the files contained in the phar. At this time, we need to restore the phar file. If we just have a look, we can use some IDE tools, such as phpstorm 10, you can open it directly. If you need to modify it, you need to extract it. For demonstration, we download a composer.phar and put it in the server directory, and create a get.php file in the root directory. The content is as follows:
<?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); //另外再提取一份,和上行二选一即可
Use a browser to access this file to extract it. The above example shows two extraction methods: the second line will create a composer directory and put the extracted content into it. The third line will generate a composer.zip file, which can be decompressed. Extracted and restored project files are available.
Replenish:
1. When deploying the phar file to the production server, you need to adjust the server configuration to avoid the browser directly downloading the phar file when accessed
2. You can set an alias for the archive. The alias is saved permanently in the archive file. It can refer to the archive with a short name regardless of where the archive file is stored in the file system. Set the alias:
$phar = new Phar('lib/yunke.phar', 0); $phar->setAlias ( "yun.phar");
After setting the alias, you can use it as follows:
<?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, which is actually a piece of PHP execution code. It can be set when making the 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 like Include it and run it just like a normal php file, but the stub code will not be executed when you directly include a file in the archive using phar://. The stub file often requires other files to be run. The restriction on the stub file is only to end with __HALT_COMPILER();. The default stub is designed to run without the phar extension. It extracts the contents of the phar file into a temporary Directory and then execute, but starting from php5.3, the extension is enabled by default
4. The created phar file cannot be modified, 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. When the archive alias is not set, it can be used to set the alias and open a reference mapped to the phar stream.
Related recommendations:
How to use the archive format phar for PHP development
How to use the phar package PHP
Detailed tutorial on using phar package in php
The above is the detailed content of PHP development archive format phar file concept and usage. For more information, please follow other related articles on the PHP Chinese website!