How PHP operates Phar files

Source: Internet
Author: User
Tags jquery library
This time to everyone to bring PHP how to operate the Phar File, PHP operation Phar File Note what, the following is the actual case, together to see.

In this paper, we describe the concept and usage of the archive format Phar file for PHP development. Share to everyone for your reference, as follows:

A PHP application is often composed of multiple files, if it is possible to centralize them as a file for distribution and operation is very convenient, such examples are many, such as the Windows operating system above the installation program, a jquery library and so on, in order to do this PHP uses the Phar document file format , this concept originates from the Java jar, but at design time, the main target for PHP's WEB environment, unlike the jar archive, is that the Phar Archive can be processed by PHP itself, so no additional tools are needed to create or use it, and PHP scripts can be used to create or extract it. Phar is a synthetic word, made up of PHP and archive, and can be seen as the meaning of the PHP archive file.

For Phar's official website document, please see http://php.net/manual/zh/book.phar.php, this document can be regarded as complementary with official website documents

There are three formats for the Phar Archive: Tar archive, zip archive, Phar Archive , the first two executions require PHP installation Phar extension support, and less, which is mainly about Phar archive format.

The Phar Format archive can be executed directly, and its production depends on the Phar extension, which is generated by the PHP script that it writes.

The Phar extension is not a fresh concept for PHP, and php5.3 has been built in PHP, originally written in PHP and named Php_archive, and then added to the PEAR Library in 2005. Because the pure PHP solution to this problem was very slow in practice, 2007 was rewritten as a pure C language extension, with the addition of support for Arrayaccess objects that use SPL to traverse the Phar archive. Since then, a lot of work has been done to improve the performance of Phar archiving.

Phar extension relies on the PHP flow wrapper, refer to the previous article PHP flow streams, wrapper wrapper concept and usage examples

Many PHP applications are distributed and run in Phar format, well-known to rely on management: composer, Unit test: PHPUnit, let's take a look at how to create, run, extract the restore.

Creation of the Phar file:

First modify the phar.readonly this option in php.ini, remove the preceding semicolon, and change the value to off, for security reasons the option is on by default, if it is disabled in php.ini (value 0 or off), then in the user script can be turned on or off, if it is open in php.ini, then The user script cannot be closed, so this is set to off to show the example.

Let's build a project, build the project folder in the root of the server as project, and the structure in the directory is as follows:

File  -yunek.js  -yunke.csslib  -lib_a.phptemplate  -msg.htmlindex.phplib.php

Where the file folder has two empty content JS and CSS files, only demo Phar can contain a variety of 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.html content is as follows:

<! DOCTYPE html>

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";

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 ()";}

The project file is ready, start creating, and now create a yunkebuild.php in the project folder sibling directory to produce the Phar format file, as follows:

<?php/** * Created by Yunke. * User:yunke * DATE:2017/2/10 * time:9:36 *///generate a Yunke.phar file $phar = new Phar (' Yunke.phar ', 0, ' Yunke.phar ');//Add Proj All files inside the ECT to the Yunke.phar archive file $phar->buildfromdirectory (dirname (file). '/project ');//Set the execution of the entry file, the first for the command line, the second for the browser access, here are set to Index.php$phar->setdefaultstub (' index.php ', ' index.php ');

The yunkebuild.php file is then accessed in the browser and a Yunke.phar file is generated, and the server root directory is structured as follows:

ProjectyunkeBuild.phpyunke.phar

This is the simplest process to produce a Phar Archive, more content please crossing network, it is important to note that if the project does not have a single execution portal, it is not appropriate to use the Phar Archive file

Use of Phar Archive files:

We set up a index.php file at the root of the server to demonstrate how to use the Phar file created above, 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 the following code is exactly the same when you do not use the archive file:

Require "project/index.php";

If there is no second row, then the third row of Yunke () will prompt undefined, so it is visible that require a Phar file is not imported all the files inside, but only imported the import execution file, but in the actual project in this portal file is often imported to use other files, In this example, the Portal execution file is project/index.php

Extract and restore Phar Files:

We sometimes curious Phar contains the file source code, this time you need to restore the Phar file, if you just look at the words can use some IDE tools, such as PHPSTORM10 can open it directly, if need to modify then need to extract operation, in order to demonstrate, We download a Composer.phar in the server directory, create a get.php file in the root directory, the contents are as follows:

<?php/** * Created by Yunke. * User:yunke * DATE:2017/2/9 * time:19:02 * * $phar = new Phar (' Composer.phar '); $phar->extractto (' composer '); Extract a copy of the original project document $phar->converttodata (Phar::zip); Another one, and the upstream two select one can

The browser to access this file, can be extracted, to show the following two methods of extraction: the second will establish a composer directory, and the extracted content into the third row to produce a composer.zip file, extract and restore the project files.

Add:

1, in the deployment of Phar files to the production server needs to adjust the configuration of the server, to avoid when access to the browser directly download Phar Files

2, you can set an alias for the archive, the alias is saved in the archive permanently saved, it can be archived with a short name reference, regardless of the archive file in the file system stored there, set aliases:

$phar = new Phar (' Lib/yunke.phar ', 0); $phar->setalias ("Yun.phar");

After setting the alias, you can use the following:

<?phprequire "Lib/yunke.phar"; require "phar://yun.phar/lib.php"; Use aliases to access archive files require "phar://lib/yunke.phar/lib.php"; Of course, you can still use this way to quote

If you do not specify an alias when you make a Phar file, you can also use the stub file to Phar::mapPhar('yunke.phar'); specify

3, the archive file has a stub file, in fact, is a piece of PHP execution code, in the production of the archive can be set, the direct execution of the archive, is actually executed it, so it is a startup file, the script contains the archive as if it contains a normal PHP file, including it and run, but directly to phar:// Contains a file in the archive does not execute the stub code, often in the stub file require contains the other files to run, the stub file is limited to HALT_COMPILER(); end, the default stub is designed to run without Phar extension, It extracts the contents of the Phar file to a temporary directory and executes it, but starting from php5.3 the extension is enabled by default built-in

4, the production of Phar files can not be changed, so the configuration files and other files need to be placed outside the archive file

5, Mapphar function: This function should only be called in stub stub code, can be used to set the alias when the archive alias is not set, open a reference map to the Phar Stream

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

How to develop a chat room with swoole and WebSocket

How to implement the refund function in PHP

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.