(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 1.0.0)
Phar::__construct — Создаёт объект Phar-архива
filenameПуть к уже существующему Phar-архиву или к архиву, который должен быть создан. Расширение в имени файла должно содержать .phar.
flagsФлаги, которые должны быть переданы в родительский класс RecursiveDirectoryIterator .
aliasПсевдоним, с помощью которого должны производиться ссылки на этот Phar-архив в вызовах, связанных с функционалом потоков.
Выбрасывает исключение BadMethodCallException , если был вызван дважды, и исключение UnexpectedValueException , если phar-архив не может быть открыт.
Пример #1 Пример использования Phar::__construct()
<?php
try {
$p = new Phar('/путь/к/my.phar', FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::KEY_AS_FILENAME,
'my.phar');
} catch (UnexpectedValueException $e) {
die('Не удалось открыть my.phar');
} catch (BadMethodCallException $e) {
echo 'Технически это не может произойти';
}
// это теперь работает
echo file_get_contents('phar://my.phar/example.txt');
// и работает так же, как если бы мы ввели
echo file_get_contents('phar:///путь/к/my.phar/example.txt');
?>Zip support seems to be shaky, in that just attempting to open a Zip file (created by 7-Zip) with both the 'zlib' and 'zip' extensions enabled renders the following error:
Error: Cannot convert phar archive "C:/Development/webdir/public_html/TestPhar.zip", unable to open entry "TestPhar/" contents: phar error: internal corruption of zip-based phar "C:/Development/webdir/public_html/TestPhar.zip" (local header of file "TestPhar/" does not match central directory)
Stick to GZ's and BZ2's (but don't forget to enable the BZ2 extension if necessary).
Phar can ONLY open executable Phar's and PharData can ONLY open non-executable Phar's. Both have the ability to convert between the two formats.
However, you can reference a file within a Phar regardless of whether it's executable using the Phar stream wrapper (file_get_contents('phar://<Phar file>/subdirectory/subdirectory/somefile.txt')).
Dustin Oprea