FileSystemWatcher::__construct

(PHP 8.6+, True Async 1.0)

public FileSystemWatcher::__construct(
    string $path,
    bool $recursive = false,
    bool $coalesce = true
)

Creates a watcher and immediately starts tracking changes. Events are buffered from the moment of creation, even if iteration has not yet begun.

Parameters

path
The path to a file or directory to watch. If the path does not exist or is inaccessible, an Error is thrown.
recursive
If true, nested directories are also monitored. Default is false.
coalesce
Event buffering mode. true (default) — events are grouped by path/filename key. Repeated changes to the same file merge the renamed/changed flags via OR. false — each OS event is stored as a separate element in a circular buffer.

Errors/Exceptions

Examples

Example #1 Watching a directory

<?php
use Async\FileSystemWatcher;

$watcher = new FileSystemWatcher('/tmp/mydir');

foreach ($watcher as $event) {
    echo "{$event->filename}\n";
    $watcher->close();
}
?>

Example #2 Recursive watching in raw mode

<?php
use Async\FileSystemWatcher;

$watcher = new FileSystemWatcher('/var/log', recursive: true, coalesce: false);

foreach ($watcher as $event) {
    echo "[{$event->path}] {$event->filename}\n";
}
?>

See Also