日期:2014-05-16 浏览次数:20666 次
$ .bin/phar.phar help终端可以直接运行。
<?php
try{
$p = new Phar(dirname(__FILE__) . "my.phar", 0, 'my.phar');
} catch (UnexpectedValueException $e) {
die('Could not open my.phar');
} catch (BadMethodCallException $e) {
echo 'technically, this cannot happen';
}
使用startBuffering来打开缓冲,对文件修改,使用缓冲的好处是不用每次修改都保存文件,提升了效率。$p->startBuffering();
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file3.txt'] = 'babyface';
$p['file3.txt']->setMetadata(42);
$p['test/time.php'] = file_get_contents('time.php');上面代码用来添加文档,添加了4个文档。最后一个time.php在test目录下面。$p->setStub("<?php
Phar::mapPhar('myphar.phar');
__HALT_COMPILER();");最后关闭缓冲区$p->stopBuffering();运行后,会在当前目录生成一个myphar.phar文件。
include 'myphar.phar';这样把phar中所有的文件都引入了。
include 'phar://myphar.phar/test/time.php';这个只把test目录下的time.php文件引入了。
echo file_get_contents('phar://my.phar/file.txt');这个会输出 hi。