How to use Phar code packaging tool in php
This article will introduce to you how to use Phar code packaging tool in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.
Briefly understand the use of Phar code packaging tool
Phar is a code packaging tool provided after PHP5 tool. Essentially, it is a code package in the form of a Java Jar file. However, since PHP itself is not compiled, this Phar actually packages the code as it is without compilation. But we can compress the packaged Phar package.
In addition, there are very few people who have actually used the Phar package. Especially today when Composer has become the de facto code library standard, Phar is even more difficult to find. However, the Composer installation package itself is also a .phar packaging file.
The main reason is that the installation of code packages in the form of Phar is not as simple and convenient as Composer. On the other hand, early programmers, especially LAMP/LAMP programmers, like Go copy the open source code instead of using a toolkit directly.
After all, having the source code in hand makes us more at ease. In fact, even if Composer directly downloads the source code, no one has ever actually looked through it. The biggest advantages of Composer compared to Phar are automatic loading of code and standard PSR command space and directory specifications. These two are not available in Phar, so we must require them if we want to use the Phar package.
Although it is outdated, we still briefly learn about it. Maybe we will be able to use it when, especially when encapsulating some internal public library functions, Phar's way of packaging code is still very useful.
Code packaging
We first create a directory tree in a standard format.
In this directory tree, the src directory stores the source code, and the build directory is used to store the generated .phar code package.
// index.php <?php require_once "phar://myphar.phar/common.php";
In the index.php file, we simply reference common.php. Note that the phar pseudo-protocol is used to load the common.php file. We have previously explained the content of pseudo-protocols in an article.
<?php // common.php class Manager{ public static function run($config){ echo "AAA", PHP_EOL; var_dump($config); } public static function ChineseMobile($mobile){ if(preg_match("/^1[34578]\d{9}$/", $mobile)){ return true; } return false; } }
The common.php file only provides a class and two simple methods for testing. The run() method simply outputs the printed content and the passed parameters. The ChineseMobile() method is a function we provide to determine our domestic mobile phone number.
[database] host=localhost db=dbname user=myuser pass=dbpass
config.ini is a configuration file. In fact, we can read the configuration file directly in the Phar code, or we can build the configuration file to the specified directory along with the code.
The source code file is ready, and the next step is to prepare the packaged compiled file.
// create-phar.php $srcRoot = "./myphar/src"; $buildRoot = "./myphar/build"; $phar = new Phar($buildRoot . "/myphar.phar", FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME, "myphar.phar"); $phar["index.php"] = file_get_contents($srcRoot . "/index.php"); $phar["common.php"] = file_get_contents($srcRoot . "/common.php"); $phar->setStub($phar->createDefaultStub("index.php")); copy($srcRoot . "/config.ini", $buildRoot . "/config.ini");
The code is not complicated. It is mainly a Phar class. This class needs to specify the directory and file name of the generated file, and then use the createDefaultStub() method to call the entry file index.php of our package. This method is A stub for creating the specified .phar file. In fact, it is to specify an entry file, just like the main() method entry in Java.
Then we copied the config.ini file to the release directory build.
Then use the command line to run the create-phar.php file to generate this code package.
# php ./create-phar.php
Use a text editor to open the myphar.phar file. We will find that it is still the familiar PHP code. Pull it to the bottom and you will find index.php and The contents of common.php are compiled into this file. The automatically generated codes above are some bootstrapping or pre-prepared statements, which are prepared for us by the Phar extension. All source codes written by users will be at the bottom of this file. In other words, you can download the Composer installation package, which is the .phar file, and see what is written in it.
The next step is to use it, this is very simple.
$config = parse_ini_file("./myphar/build/config.ini"); require './myphar/build/myphar.phar'; Manager::run($config); // AAA // array(4) { // ["host"]=> // string(9) "localhost" // ["db"]=> // string(6) "dbname" // ["user"]=> // string(6) "myuser" // ["pass"]=> // string(6) "dbpass" // } var_dump(Manager::ChineseMobile('13811111111')); var_dump(Manager::ChineseMobile('138111111112')); // bool(true) // bool(false)
Compression ability
As mentioned before, as a code library, Phar has long been defeated by Composer, but in addition to being used as some installation packages In addition to being used, Phar itself is also a compression tool. It can be used to archive some files, texts, directories and the like. Next, let me briefly take a look at how Phar is used for text archiving.
unlink('./my.phar'); unlink('./my.phar.bz2'); unlink('./my.phar.gz'); $p = new Phar('./my.phar', 0 ,'my.phar'); $p['myfile1.txt'] = 'hi1'; $p['myfile2.txt'] = 'hi2'; $p1 = $p->compress(Phar::GZ); $p2 = $p->compress(Phar::BZ2); unset($p); $decompressPhar = new Phar('./my.phar', 0 ,'my.phar'); foreach($decompressPhar as $file){ // $file 是返回的 PharFileInfo 对象 var_dump($file->getFileName()); var_dump($file->isCompressed()); var_dump($file->isCompressed(Phar::BZ2)); var_dump($file->isCompressed(Phar::GZ)); var_dump($file->getContent()); } echo '==================', PHP_EOL; // string(11) "myfile1.txt" // bool(false) // bool(false) // bool(false) // string(3) "hi1" // string(11) "myfile2.txt" // bool(false) // bool(false) // bool(false) // string(3) "hi2"
首先,依然是实例化一个 Phar 类,然后我们给它像数组一样增加属性,这样,属性内容就被打包进了 .phar 文件中。通过直接查看 my.phar 文件,我们可以看出,myfile1.txt 这两个属性直接被写成了文件进行保存了,也就是说,它帮我们将文本转化成文件并打包在了 my.phar 这个压缩包文件中了。
compress() 方法则是将当前的这个 Phar 对象压缩存储为某个格式的文件。这里我们直接压缩了 Bzip2 和 GZ 文件。调用这个方法后直接就会生成对应的压缩文件。
Phar 对象在遍历时产生的对象是 PharFileInfo 对象,它拥有很多类似于 File 的文件操作函数。大家可能在官方文档中找到相关的说明。
假设我们遍历 my.phar.gz ,内容依然可以正常输出,但循环中的 isCompressed() 判断都依然会是 false ,难道文件没有被压缩吗?其实,我们需要通过另一个函数来让所有文件都进行统一格式的压缩。
$p = new Phar('./my.phar', 0 ,'my.phar'); $p->compressFiles(Phar::GZ); unset($p); $decompressPhar = new Phar('./my.phar.gz', 0 ,'my.phar'); foreach($decompressPhar as $file){ // $file 是返回的 PharFileInfo 对象 var_dump($file->getFileName()); var_dump($file->isCompressed()); var_dump($file->isCompressed(Phar::BZ2)); var_dump($file->isCompressed(Phar::GZ)); var_dump($file->getContent()); } echo '==================', PHP_EOL; // string(11) "myfile1.txt" // bool(true) // bool(false) // bool(true) // string(3) "hi1" // string(11) "myfile2.txt" // bool(true) // bool(false) // bool(true) // string(3) "hi2"
使用 compressFiles() 对整个 .phar 中的所有文件进行了统一的格式压缩之后,再打印时 isCompressed() 就会返回对应格式的 true 了。
数据格式 Phar
最后,如果只是为了打包压缩功能的话,我们没必要使用 Phar 类。Phar 类最主要的还是用来打包能够运行的 PHP 源码,也就是它的 createDefaultStub() 方法非常重要。而如果只是打包普通文件的话,我们并不需要这个方法,这时,我们就可以使用另外一个 PharData 类来进行数据的打包压缩。使用方法和 Phar 类是一模一样的。同时,PharData 类可以直接打包成 tar 之类的文件。
$p = new PharData('./myData.tar'); $p['myfile1.txt'] = 'hi1'; $p['myfile2.txt'] = 'hi2'; foreach($p as $file){ var_dump($file->getFileName()); var_dump($file->isCompressed()); var_dump($file->isCompressed(Phar::BZ2)); var_dump($file->isCompressed(Phar::GZ)); var_dump($file->getContent()); } echo '==================', PHP_EOL; // string(11) "myfile1.txt" // bool(false) // bool(false) // bool(false) // string(3) "hi1" // string(11) "myfile2.txt" // bool(false) // bool(false) // bool(false) // string(3) "hi2"
总结
说实话,Phar 真的是一个冷门项目,但是在某些情况中又非常有用,比如它虽然在代码包领域被 Composer 打败了,但是它又可以成为 Composer 的安装包,也就是说,没有 Phar 你就安装不了 Composer 。而做为压缩工具,虽然有强大的实力但使用的却也非常的少。因此,我们还是以了解为目的,如果感觉某些场景非常合适的话,也完全可以深入的研究拿来放到我们的实际项目中使用。毕竟它是 PHP 的一部分,不需要任何的编译安装及其它支持,非常原生。
测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202007/source/%E7%AE%80%E5%8D%95%E4%BA%86%E8%A7%A3Phar%E4%BB%A3%E7%A0%81%E6%89%93%E5%8C%85%E5%B7%A5%E5%85%B7%E7%9A%84%E4%BD%BF%E7%94%A8.php
推荐学习:php视频教程
The above is the detailed content of How to use Phar code packaging tool in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
