「php化整为零系列」十、Phar


源码地址:https://github.com/wilfordw/phpTutorial

该系列我只写我的理解,非官方解释,如不够专业请见谅

PHAR: 即 PHP Archive,将这个应用程序打包成单个文件,以利于分发和安装的机制,似乎是从JAVA的JAR借鉴来的东西

开启phar功能

配置php.ini

[Phar]
; http://php.net/phar.readonly
phar.readonly = Off//默认是On

基本语法

  1. 打包
new Phar(包名)
$phar->buildFromDirectory(打包目录, 正则筛选);
$phar->compressFiles( Phar::GZ |PHAR::BZ2);//压缩方式
$phar->setStub( $phar->createDefaultStub(入口文件) );
  1. 引用
require_once 'phar:://包名/文件';

Example

  • 目录结构
  • 代码
//user.class.php
<?php
 
class user {
    private $name="anonymous";
    private $email="anonymous@nonexists.com";
 
    public function set_email($email) {
        $this->email=$email;
    }
    public function set_name($name) {
        $this->name=$name;
    }
    public function introduce() {
        echo "My name is $this->name and my email address is $this->email.\n";
    }
 
}
//user.func.php
<?php
 
require_once "user.class.php";
 
function make_user($name,$email) {
    $u=new user();
    $u->set_name($name);
    $u->set_email($email);
    return $u;
}
 
function dump_user($u) {
    $u->introduce();
}
//test.php
<?php
require_once "user.class.php";
 
$u=new user();
$u->set_name("laomeng");
$u->set_email("laomeng@163.com");
$u->introduce();

通过make_phar.php打包成phar
命令行执行php make_phar.php在目录下就会生成user.phar

//make_phar.php
<?php
$phar = new Phar('user.phar');#定义压缩包名
//指定压缩的目录,第二个参数可通过正则来制定压缩文件的扩展名
$phar->buildFromDirectory(dirname(__FILE__) . '/user', '/\.php$/');
$phar->setStub($phar->createDefaultStub('test.php'));//设置启动加载的文件
$phar->compressFiles(Phar::GZ);#表示使用gzip来压缩此文件。也支持bz2压缩。参数修改为 PHAR::BZ2即可

测试代码

//test_phar.php
<?php
require_once "user.phar";//加载压缩包
#My name is laomeng and my email address is laomeng@163.com.来自入口test.php
require_once "phar://user.phar/user.class.php";//加载压缩包内php文件

$u=new user();
$u->set_name("mengguang");
$u->set_email("mengguang@gmail.com");
$u->introduce();#My name is mengguang and my email address is mengguang@gmail.com.
 
require_once "phar://user.phar/user.func.php";
 
$u=make_user("xiaomeng","xiaomeng@163.com");
dump_user($u);#My name is xiaomeng and my email address is xiaomeng@163.com.

命令行执行php test_phar.php
输出

My name is laomeng and my email address is laomeng@163.com.
My name is mengguang and my email address is mengguang@gmail.com.
My name is xiaomeng and my email address is xiaomeng@163.com.

如此就介绍完了phar的简单用法,个人感觉相对要比打jar包要复杂,不过引用要比jar包简单的多,可能功能上有些缺失,不过掌握以上的功能已经够用了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Welcome 目前网络上充斥着大量的陈旧信息,让PHP新手误入歧途,传播着错误的实践和糟糕的代码,这必须得到纠正...
    layjoy阅读 21,792评论 7 118
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,986评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,833评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,122评论 6 342
  • 【绘制心得】 模拟测试的成绩不是很好,感觉今年的大师梦要碎了。于是我便思考,是否要顶住生活、家人的压力,坚...
    焦典I世界记忆大师阅读 1,679评论 3 1

友情链接更多精彩内容