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

Phar::startBufferingStart buffering Phar write operations, do not modify the Phar object on disk


public Phar::startBuffering(): void

Although technically unnecessary, the Phar::startBuffering() method can provide a significant performance boost when creating or modifying a Phar archive with a large number of files. Ordinarily, every time a file within a Phar archive is created or modified in any way, the entire Phar archive will be recreated with the changes. In this way, the archive will be up-to-date with the activity performed on it.

However, this can be unnecessary when simply creating a new Phar archive, when it would make more sense to write the entire archive out at once. Similarly, it is often necessary to make a series of changes and to ensure that they all are possible before making any changes on disk, similar to the relational database concept of transactions. the Phar::startBuffering()/Phar::stopBuffering() pair of methods is provided for this purpose.

Phar write buffering is per-archive, buffering active for the foo.phar Phar archive does not affect changes to the bar.phar Phar archive.

Elenco dei parametri

Questa funzione non contiene parametri.

Valori restituiti

Nessun valore viene restituito.


Example #1 A Phar::startBuffering() example

// make sure it doesn't exist
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar'0'brandnewphar.phar');
} catch (
Exception $e) {
'Could not create phar:'$e;
'The new phar has ' $p->count() . " entries\n";
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file3.txt'] = 'babyface';
function __autoload(
    include 'phar://myphar.phar/' . str_replace('_', '/', 
$class) . '.php';
include 'phar://myphar.phar/startup.php';

Vedere anche:

  • Phar::stopBuffering() - Stop buffering write requests to the Phar archive, and save changes to disk
  • Phar::isBuffering() - Used to determine whether Phar write operations are being buffered, or are flushing directly to disk

add a note add a note

User Contributed Notes 1 note

anon at php dot com
1 year ago
To setthe  ini key phar.readonly to 0, from a unix shell script, this is possible on the fly it is possible see below.
The example given on this page has some issues with quoting: (See conversion to simple quotes)

#!/usr/bin/php -d phar.readonly=0
print(ini_get('phar.readonly')); // Must return 0
// make sure it doesn't exist
try {
$p = new Phar(dirname(__FILE__) . '/brandnewphar.phar', 0, 'brandnewphar.phar');
} catch (
Exception $e) {
'Could not create phar:', $e;
'The new phar has ' . $p->count() . " entries\n";
$p['file.txt'] = 'hi';
$p['file2.txt'] = 'there';
$p['file3.txt'] = 'babyface';
function __autoload($class)
    include "phar://myphar.phar/" . str_replace("_", "/", $class) . ".php";
include "phar://myphar.phar/startup.php";

// Test
$m = file_get_contents("phar://brandnewphar.phar/file2.txt");
$m = explode("\n",$m);
/* Output:
* there
To Top