Tutorial on using Phar Package in PHP

Source: Internet
Author: User

Phar Introduction:
The Phar archive concept comes from the Java technology JAR Archive, which allows you to package your application with a single file that contains everything you need to run your application. This file differs from a single executable file, which is typically generated by a programming language, such as C, because the file is actually an archive file rather than a compiled application. So the JAR file actually contains the files that make up the application, but with security in mind, these files are not carefully differentiated. The Phar extension is based on a similar concept, but is designed primarily for PHP's WEB environment. Similarly, unlike JAR Archives, Phar archives can be processed by PHP itself, so there is no need to use additional tools to create or use them.

The Phar extension is not a fresh concept for PHP. It was originally written in PHP and named Php_archive, and was added to the PEAR Library in 2005. In practice, however, the pure PHP solution to this problem was very slow, so the 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.

Create Phar
There are several steps you need to take to create a Phar file. All steps need to be created with some form of PHP command, because there is no standalone tool for creating an archive.

The following article mainly introduce to you about PHP Phar Package use of relevant data, the article is described or relatively detailed, the need for friends below to see it together.

Objective

The Java-like jar package, named Phar, is supported after PHP5.3. Used to package multiple PHP files into a single file.

First you need to modify the php.ini configuration will Phar ReadOnly shutdown, default is not write Phar Package, include is enabled by default.

Phar.readonly = On

Create a Phar Compressed package

<?php$phar = new Phar (' Swoole.phar '); $phar->buildfromdirectory (DIR. '/). /', '/\.php$/'); $phar->compressfiles (PHAR::GZ); $phar->stopbuffering (); $phar->setstub ($phar Createdefaultstub (' lib_config.php '));

new PharThe parameter is the name of the compressed package. BUILDFROMDIRECTORY Specifies the compressed directory, and the second parameter can be used to make a regular extension of the compressed file.

Phar::GZRepresents the use of gzip to compress this file. BZ2 compression is also supported. Modify the parameters to be PHAR::BZ2 able.

The setsub is used to set boot loaded files. Lib_config.php is automatically loaded and executed by default.

After executing this code, a Swoole.phar file is generated.

Using Phar to compress packages

<?phpinclude ' Swoole.phar '; include ' swoole.phar/code/page.php ';

With Phar You can easily package your code and integrate it into your online machine.

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.