[php] phar

build.php打包www目录:

<?php
class A{
    public $a = 1;
}
$p = new Phar('test.phar',0,'test.phar');
$p->buildFromDirectory(dirname(__FILE__)."/www");
//访问phar包外面的文件(require "test.phar")将会执行stub设置的内容
//$p->setDefaultStub('a.php','a.php');
//或
//$stub = $p->createDefaultStub('a.php','a.php');;
//$p->setStub($stub);
//或
$p->setStub("<?php echo 'sssssssssss';__HALT_COMPILER();?>");//__HALT_COMPILER();结尾
//设置metadata,序列化形式保存在phar包里,使用phar包的时候会自动反序列化
$p->setMetadata(new A);
var_dump($p->getMetadata());
?>

index.php中包含phar包:

<?php
class A{
    function __destruct(){
        echo "destruct";
    }
}
require "test.phar";
?>

访问 build.php 进行打包:

[php] phar

生成 test.phar

[php] phar

test.phar包:

[php] phar

 

访问 index.php

[php] phar

上面通过 require "test.phar" 的方式引用了phar包里的所用文件

“ssssssssss”是设置的stub,“执行” 包时首先执行stub;“destruct” 是 new A对象反序列化时执行的__destruct 函数的输出。

以上析构函数__destruct的执行利用了phar的metadata保存序列化且使用phar包的时候反序列化的特性。

另一种 phar 包的利用:include "phar://test.phar/b/b.php"

[php] phar

 

 

修改后缀后同样可以:

[php] phar

参考:

http://blog.csdn.net/wang740209668/article/details/52751915

http://blog.csdn.net/u011474028/article/details/54973571

http://www.91ri.org/13363.html

相关文章:

  • 2021-12-14
  • 2022-12-23
  • 2022-01-12
  • 2021-12-24
  • 2021-11-29
  • 2022-02-09
猜你喜欢
  • 2022-12-23
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2021-09-19
相关资源
相似解决方案