PHP Luminova: Asynchronous Timeout Executions & Time Intervals
Run a callback repeatedly from a PHP loop with a Fiber-based interval.
Interval uses a Fiber to suspend between callbacks. The caller drives the interval with tick() or run(). Each resume waits for the configured delay before invoking the callback.
Usage Examples
Basic Usage
The factory runs the first callback before returning. Each call to tick() runs another iteration.
use Luminova\Time\Interval;
$interval = Interval::setInterval(static function (?int $iteration): void {
if ($iteration !== -1) {
echo 'Iteration ' . ($iteration ?? 0) . PHP_EOL;
}
}, 1);
$interval->tick(1);
$interval->clear(-1);Starting from a New Instance
Construct the interval to prepare the Fiber, then use run() to trigger the first callback.
use Luminova\Time\Interval;
$interval = new Interval(static function (?string $message): void {
if ($message !== null) {
echo $message . PHP_EOL;
}
}, 1);
$interval->run('Started');
$interval->clear();Passing State Between Ticks
Pass a value to tick() to change what the callback does on each iteration. Here the callback ignores the stop signal passed to clear().
use Luminova\Time\Interval;
$interval = new Interval(static function (?int $index): void {
if ($index === null) {
return;
}
echo "Tick {$index}" . PHP_EOL;
if ($index === 3) {
echo 'Third tick reached.' . PHP_EOL;
}
}, 1);
for ($index = 1; $index <= 5; $index++) {
$interval->tick($index);
}
$interval->clear();Class Definition
- Class Namespace:
Luminova\Time\Interval
Methods
constructor
Creates and starts the Fiber, which suspends before the first callback.
public function __construct(callable $callback, int $milliseconds)Parameters:
| Parameter | Type | Description |
|---|---|---|
$callback | callable | The callback function to execute at each interval. |
$milliseconds | int | Delay in seconds in the current implementation. |
setInterval
Creates an interval and runs its first callback.
public static function setInterval(callable $callback, int $milliseconds): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$callback | callable | The callback function to execute on each interval. |
$milliseconds | int | Delay in seconds in the current implementation. |
Return:
self - Returns the new interval after its first callback.
Throws:
- Luminova\Exceptions\RuntimeException - Throws if an error occurs during execution.
run
Starts or resumes the execution of the interval.
Resumes a suspended interval for one iteration. Use tick() for later iterations.
public function run(mixed $value = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | An optional value to pass to the callback (default: null). |
clear
Stops the interval.
If the Fiber is suspended, this method resumes it so it can exit. A callback can receive the supplied stop value during that resume.
public function clear(mixed $value = null): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | An optional value to pass to the callback during the clear operation (default: null). |
tick
Resumes the interval execution manually.
This method should be called periodically to resume the interval fiber and allow the next execution of the callback.
public function tick(mixed $value = null): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$value | mixed | An optional value to pass to the callback when resuming (default: null). |
Return:
bool - Return true if interval is running and invocation succeed, false otherwise.
isRunning
Checks if the interval is currently running.
Use this to check whether the interval is marked as active.
public function isRunning(): boolReturn:
bool - Return true if the interval is running, otherwise false.