<!DOCTYPE html>
The 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";
The 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 to be created. Now, a yunkeBuild. php file is created in the same directory of the project folder to generate a phar file. The content is as follows:
<? Php/*** Created by yunke. * User: yunke * Date: * Time: * // generates a yunke. phar File $ phar = new Phar ('yunke. phar ', 0, 'yunke. phar '); // Add all files in the project to yunke. phar archive FILE $ phar-> buildFromDirectory (dirname (_ FILE __). '/Project'); // set the entry file for execution. The first file is used for command line and the second file is used for browser access. index is set here. php $ phar-> setDefaultStub ('index. php', 'index. php ');
Access the yunkeBuild. php file in the browser, and a yunke. phar file is generated. The root directory structure of the server is as follows:
projectyunkeBuild.phpyunke.phar
This is the simplest process of generating a phar archive file. For more information, see the official website. Note that if the project does not have a single execution portal, you should not use the phar archive file.
Use of phar archive files:
Create an index. php file in the root directory of the server 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 the index. php file contains only the first line, add the following code exactly the same as if the archive file is not used:
require "project/index.php";
If there is no second line, the yunke () in the third line will prompt undefined. Therefore, when a phar file in require is not imported, however, only the entry execution file is imported, but in actual projects, other files are often imported into the entry file. In this example, the entry execution file is project/index. php
Extraction and restoration of phar files:
Sometimes we will be curious about the file source code contained in phar. At this time, we need to restore the phar file. If we just have a look at it, we 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. put phar in the server directory and create a get. PHP file with the following content:
<? Php/*** Created by yunke. * User: yunke * Date: * Time: */$ phar = new Phar ('composer. phar '); $ phar-> extracloud ('composer'); // extract an original project file $ phar-> convertToData (Phar: ZIP ); // extract another copy, and select either from the upstream version.
Access the file in a browser and extract the file. The preceding columns show two extraction methods: the second row creates a composer, and stores the extracted content. The third row extracts and restores a composer.zip file.
Supplement:
1. You need to adjust the server configuration when deploying the phar file to the production server to avoid directly downloading the phar file from the browser when accessing the server.
2. You can set an alias for an archive object. The alias is permanently saved in the archive object. It can be referenced with a short name, regardless of where the archive object is stored in the file system, set alias:
$phar = new Phar('lib/yunke.phar', 0);$phar->setAlias ( "yun.phar");
The alias can be used as follows:
<? Phprequire "lib/yunke. phar "; require" phar: // yun. phar/Lib. php "; // use an alias to access the archive file require" phar: // lib/yunke. phar/Lib. php "; // Of course, you can still reference it in this way
If you do not specify an alias when creating a phar file, you can also usePhar::mapPhar('yunke.phar');
Specify
3. There is a stub file in the archive file, which is actually a piece of php code to execute. You can set it when creating the archive file. When you directly execute the archive file, you actually execute it, so it is the Startup File; when a script contains an archive file, it will include it and run it like a normal PHP file, but it will not execute the stub code when it directly contains a file in phar, the require file in the stub file usually contains other files to be run.__HALT_COMPILER();
The default stub is designed to run without a phar extension. It extracts the phar file content to a temporary directory and then executes it. However, the extension is enabled by default starting from php5.3.
4. The created phar file cannot be modified. Therefore, files such as configuration files must be placed outside the archive file.
5. mapPhar function: This function should only be called in the stub code. It can be used to set the alias when the archive alias is not set and map a reference to the phar stream.