PHP8 PharFileInfo::__construct

2024-02-22 11:46 更新

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 1.0.0)

PharFileInfo::__construct — 构造一个 Phar 入口对象

说明 

public PharFileInfo::__construct(string $filename)

这不应该直接调用。而是 PharFileInfo 对象 通过数组访问调用 Phar::offsetGet() 进行初始化。

参数 

filename

用于检索文件的完整 URL。如果您希望检索信息 对于来自 phar 的文件, 条目应为 。my/file.phpboo.pharphar://boo.phar/my/file.php

错误/异常 

如果 __construct() 被调用两次,则引发 BadMethodCallException。 如果出现以下情况,则引发 UnexpectedValueException 请求的 phar URL 格式不正确,请求的 无法打开 phar,或者在 Phar 中找不到文件。

示例 

示例 #1 A PharFileInfo::__construct() example

<?php
try {
    $p = new Phar('/path/to/my.phar', 0, 'my.phar');
    $p['testfile.txt'] = "hi\nthere\ndude";
    $file = $p['testfile.txt'];
    foreach ($file as $line => $text) {
        echo "line number $line: $text";
    }
    // this also works
    $file = new PharFileInfo('phar:///path/to/my.phar/testfile.txt');
    foreach ($file as $line => $text) {
        echo "line number $line: $text";
    }
} catch (Exception $e) {
    echo 'Phar operations failed: ', $e;
}
?>

以上示例会输出:

line number 1: hi
line number 2: there
line number 3: dude
line number 1: hi
line number 2: there
line number 3: dude


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号