Home > Backend Development > PHP Tutorial > PHP development archive format phar file concept and usage (example)

PHP development archive format phar file concept and usage (example)

墨辰丷
Release: 2023-03-26 09:00:02
Original
1347 people have browsed it

This article mainly introduces the concept and usage of archive format phar files developed in PHP. It analyzes the creation, use, unpacking, restoration and extraction of archive format phar files in detail in the form of examples and other concepts and operation implementation methods. Friends in need You can refer to the following

The example of this article describes the concept and usage of the archive format phar file in PHP development. Share it with everyone for your reference, the details are as follows:

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. Examples of this include There are many, such as the installation program on the window operating system, a jquery library, etc. In order to achieve this, PHP adopts the phar document file format. This concept is derived from the jar of java, but it is mainly designed for the Web environment of PHP. 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 PHP scripts. phar is a compound word composed of PHP and Archive. It can be seen that it means PHP archive file.

For the official website documentation of phar, please see http://php.net/manual/zh/book.phar.php. This document can be seen as complementary to the official website documentation

Phar archive files have three formats: tar archive, zip archive, 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 depends 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 and support for traversing Phar archives using SPL's ArrayAccess objects was added. 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 stream 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 for security reasons The reason is that this option is on by default. If it is disabled in php.ini (the value is 0 or off), it can be turned on or off in user scripts. If it is turned on in php.ini, then user scripts 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
Copy after login

where The file folder has two js and css files with empty contents. 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()";
}
Copy after login

msg.html content is as follows:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>phar</title>
</head>
<body>
<?=$str; ?>
</body>
</html>
Copy after login

index.php content 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";
Copy after login

Lib.php content is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:20
 */
function yunke()
{
  echo "l am yunke()";
}
Copy after login

The project file is ready, start creating, now create a yunkeBuild in the same level directory of the project folder .php, used to generate phar format files, the content is as follows:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/10
 * Time: 9:36
 */
//产生一个yunke.phar文件
$phar = new Phar(&#39;yunke.phar&#39;, 0, &#39;yunke.phar&#39;);
// 添加project里面的所有文件到yunke.phar归档文件
$phar->buildFromDirectory(dirname(__FILE__) . &#39;/project&#39;);
//设置执行时的入口文件,第一个用于命令行,第二个用于浏览器访问,这里都设置为index.php
$phar->setDefaultStub(&#39;index.php&#39;, &#39;index.php&#39;);
Copy after login

Then access this yunkeBuild.php file in the browser, a yunke.phar will be generated file. At this time, the server root directory structure is as follows:

project
yunkeBuild.php
yunke.phar
Copy after login

This is the simplest process to generate a phar archive file. For more information, please see the official website, here It should be noted that if the project does not have a single execution entry, it is not appropriate to use the phar archive file

Use of phar archive file:

We create an index in the server root directory. php file to demonstrate how to use the phar file created above, with the following content:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/8
 * Time: 9:33
 */
require "yunke.phar";
require "phar://yunke.phar/Lib.php";
yunke();
Copy after login

If there is only the first line in the index.php file, then and do not use archiving When adding the file, add the following code exactly the same:

require "project/index.php";
Copy after login

If there is no second line, then the yunke() on the third line will prompt that it is undefined, so the require is visible A phar file does not import all the files in it, but only the entry execution file. However, in actual projects, other files that need to be used are often imported into this entry file. In this example, the entry execution file is project/ index.php

Extract and restore the phar file:

We sometimes wonder about the source code of the files contained in phar. At this time, we need to restore the phar file. If it is just To take a look, you can use some IDE tools, such as phpstorm 10 to 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 in the root directory. php file with the following content:

<?php
/**
 * Created by yunke.
 * User: yunke
 * Date: 2017/2/9
 * Time: 19:02
 */
$phar = new Phar(&#39;composer.phar&#39;);
$phar->extractTo(&#39;composer&#39;); //提取一份原项目文件
$phar->convertToData(Phar::ZIP); //另外再提取一份,和上行二选一即可
Copy after login

用浏览器访问这个文件,即可提取出来,以上列子展示了两种提取方式:第二行将建立一个composer目录,并将提取出来的内容放入,第三行将产生一个composer.zip文件,解压即可得到提取还原的项目文件。

补充:

1、在部署phar文件到生产服务器时需要调整服务器的配置,避免当访问时浏览器直接下载phar文件

2、可以为归档设置别名,别名保存在归档文件中永久保存,它可以用一个简短的名字引用归档,而不管归档文件在文件系统中存储在那里,设置别名:

$phar = new Phar(&#39;lib/yunke.phar&#39;, 0);
$phar->setAlias ( "yun.phar");
Copy after login

设置别名后可以如下使用:

<?php
require "lib/yunke.phar";
require "phar://yun.phar/Lib.php"; //使用别名访问归档文件
require "phar://lib/yunke.phar/Lib.php"; //当然仍然可以使用这样的方式去引用
Copy after login

如果在制作phar文件时没有指定别名,也可以在存根文件里面使用Phar::mapPhar('yunke.phar');指定

3、归档文件中有一个存根文件,其实就是一段php执行代码,在制作归档时可以设置,直接执行归档文件时,其实就是执行它,所以它是启动文件;在脚本中包含归档文件时就像包含普通php文件一样包含它并运行,但直接以phar://的方式包含归档中某一个文件时不会执行存根代码, 往往在存根文件里面require包含要运行的其他文件,对存根文件的限制仅为以__HALT_COMPILER();结束,默认的存根设计是为在没有phar扩展时能够运行,它提取phar文件内容到一个临时目录再执行,不过从php5.3开始该扩展默认内置启用了

4、制作的phar文件不能被改动,因此配置文件之类的文件需要另外放置在归档文件外面

5、mapPhar函数:这个函数只应该在stub存根代码中调用,在没有设置归档别名的时候可以用来设置别名,打开一个引用映射到phar流

相关推荐:

PHP如何操作phar文件

phar扩展来节省空间 的方法

PHP开发之归档格式phar文件概念与用法

The above is the detailed content of PHP development archive format phar file concept and usage (example). 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