Simply use PHP Phar to package PHP code notes

Source: Internet
Author: User
Tags stack trace

Phar Introduction:
The concept of the Phar archive comes from the JAR archive of Java™ Technology, which allows the application to be packaged using a single file containing everything needed to run the 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.

1.
Before using Phar You need to modify Phar.readonly = off or 0, 1 in php.ini, otherwise the following error will be reported:
Fatal error:uncaught exception ' unexpectedvalueexception ' with message ' cannot write to archive-write operations Restri CTED by INI setting ' in D:\www\test\edu_phar\build.php:5 Stack trace: #0 D:\www\test\edu_phar\build.php (5): phar-> Buildfromdirectory (' D:\www\test\edu ... ') #1 {main} thrown in D:\www\test\edu_phar\build.php on line 5

Figure 1
Note: You do not need to use this setting when opening and referencing files within PHP's Phar archive.

2.
Look at our packaged program, I simply wrote a demo called blog, the following is the program Listing 2, a total of 4 files and two folders.

Figure 2
Take a look at the entry file index.php 3:

Figure 3
Let's run a little bit. See: 4

Figure 4

The output can be a classic Hello world!

Let's add the parameters to the run. Look at: 5

Figure 5

Demo will work, no problem

3.
Preparing the Packager build.php 6

Figure 6

build.php File Code 7

Figure 7

4.
Start packaging, through the command line mode into the directory of the build.php file, execute PHP build.php, if there is no error description packaging completed, 8. You will find that you have generated the Blog.phar file 9

Figure 8

Figure 9

5.
We'll use this packaging file when we're done packing. I created a new directory Testphar and copied the newly packaged Blog.phar file.
I'm building a new index.php, and we're using Blog.phar 10 in index.php.

Figure 10

We're running. index.php 11

Figure 11

The whole simple Phar use process is finished, is not very simple, in fact, I am also learning recently.
If there is any misleading, also ask you big Newlio pointing twos.

Simply use PHP Phar to package PHP code notes

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.